How to reset selected options in select with custom value for options in angularJS

Mohammad  picture Mohammad · Mar 23, 2016 · Viewed 8.3k times · Source

I want to reset selected options in select with custom value for options in angularJS :

HTML

<select id="" name="" class="form-control" ng-model="mymodel">
    <option value="1000">All</option>
    <option value="-1">Not Done Yet</option>
    <option value="0">Already Done</option>
    <option value="1">Other option1</option>
    <option value="2">Other option1</option>
</select>

Select option is : <span>{{mymodel}}</span>

<button class="btn btn-danger glyphicon glyphicon-refresh"
    data-toggle="tooltip" title="Reset"
    ng-click="clearSearch();">Reset</button>

JS

$scope.clearSearch = function () {
    $scope.mymodel = 1000;
}

Answer

georgeawg picture georgeawg · Mar 23, 2016

If you're using AngularJS 1.4, you will need set values to a string instead of a number.

$scope.clearSearch = function () {
    //Use a string
    $scope.mymodel = '1000';
    //Not a number
    //$scope.mymodel = 1000;
}