【发布时间】:2014-07-28 08:27:35
【问题描述】:
我对 AngularJS 有以下问题。我有一个 使用 ngOptions 创建的选项进行选择。我想要 将所选选项设置回默认选项。我尝试过了 删除模型变量,例如:
if(angular.isDefined($scope.first)){
delete $scope.first;
}
但这不起作用。这是我的html。
<div ng-app="app">
<div ng-controller="testCtrl">
<select data-ng-model="first" data-ng-options="item.name for item in selectContent" required="required">
<option value="" style="display: none;">-- select --</option>
</select>
{{first.value}}
<hr/>
<input type="button" value="reset dropdown" data-ng-click="resetDropDown()"/>
</div>
</div>
这是我的 JavaScript 代码:
angular.module('app', []).controller('testCtrl', function ($scope) {
$scope.selectContent = [
{
name: "first",
value: "1"
},
{
name: "second",
value: "2"
}
];
$scope.resetDropDown = function() {
if(angular.isDefined($scope.first)){
delete $scope.first;
}
}
});
这是工作的jsfiddle:
http://jsfiddle.net/zono/rzJ2w/
我该如何解决这个问题?
最好的问候。
【问题讨论】:
标签: angularjs