如何使用 angularJS 中选项的自定义值重置 select 中的 selected 选项
How to reset selected options in select with custom value for options in angularJS
我想用 angularJS 选项的自定义值重置 select 选项 select :
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 选项是:<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;
}
我让这个工作正常 JSFiddle。好像还不错。
请注意,我初始化了 $scope.mymodel
。
JS
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.mymodel = 1000;
$scope.clearSearch = function () {
$scope.mymodel = 1000;
}
}
如果您使用的是 AngularJS 1.4,则需要将值设置为 字符串 而不是数字。
$scope.clearSearch = function () {
//Use a string
$scope.mymodel = '1000';
//Not a number
//$scope.mymodel = 1000;
}
我想用 angularJS 选项的自定义值重置 select 选项 select :
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 选项是:<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;
}
我让这个工作正常 JSFiddle。好像还不错。
请注意,我初始化了 $scope.mymodel
。
JS
var myApp = angular.module('myApp',[]);
function MyCtrl($scope) {
$scope.mymodel = 1000;
$scope.clearSearch = function () {
$scope.mymodel = 1000;
}
}
如果您使用的是 AngularJS 1.4,则需要将值设置为 字符串 而不是数字。
$scope.clearSearch = function () {
//Use a string
$scope.mymodel = '1000';
//Not a number
//$scope.mymodel = 1000;
}