0

So there is a dropdown selector which must be set on the default option and be able to reset to it if reset button is clicked.

I managed to do it with jQuery, I'm wondering how can it be done using AngularJS

$('#buttonID').click(function(){
    $('#selectId').val('0');
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<select id="selectId">
    <option value="0">first option</option>
    <option value="1">second option</option>
    <option value="2">third option</option>
</select>
<input type="button" id="buttonID" value="reset"/>

Any suggestions?

2

1 Answer 1

0

You need to have a collection of options and a model bound to the select box. On reset, you just need to change the value of bound model to what you want:

angular.module('app', [])
  .controller('ctrl', function($scope) {
    $scope.opt = 0;
    $scope.options = [{
      value: 0,
      label: 'first option'
    }, {
      value: 1,
      label: 'second option'
    }, {
      value: 2,
      label: 'third option'
    }];
    
    $scope.reset = function() {
      $scope.opt = 0;
    };
  });
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
  <select id="selectId" ng-options="opt.value as opt.label for opt in options" ng-model="opt">
</select>
  <input type="button" id="buttonID" value="reset" ng-click="reset()"/>
</div>

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.