Mohammad
Mohammad

Reputation: 3

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

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;
}

Upvotes: 0

Views: 8804

Answers (2)

georgeawg
georgeawg

Reputation: 48968

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;
}

Upvotes: 2

Erazihel
Erazihel

Reputation: 7605

I made this working JSFiddle. It seems to work fine.

Note that I initialized the $scope.mymodel.

JS

var myApp = angular.module('myApp',[]);

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

Upvotes: 0

Related Questions