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?

Leo Messi
  • 5,157
  • 14
  • 63
  • 125
  • Check this [select](https://docs.angularjs.org/api/ng/directive/select). – Kaustubh Khare Apr 17 '18 at 11:54
  • Possible duplicate of [Setting default value in select drop-down using Angularjs](https://stackoverflow.com/questions/17815036/setting-default-value-in-select-drop-down-using-angularjs) – 4b0 Apr 17 '18 at 11:55

1 Answers1

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>
31piy
  • 23,323
  • 6
  • 47
  • 67