所以基本上我定义了以下选择框:
<div ng-controller="ExampleController">
<span class="nullable">
<select ng-model="myColor" ng-options="color.name for color in colors">
<option value="">-- choose color --</option>
</select>
</span><br/>
<div>
<input type="button" value="Reinitialize array" ng-click="switchModel()"/>
</div>
以下控制器:
angular.module('selectExample', [])
.controller('ExampleController', ['$scope', function($scope) {
$scope.colors = [
{name:'black', shade:'dark'},
{name:'white', shade:'light'},
{name:'red', shade:'dark'},
{name:'blue', shade:'dark'},
{name:'yellow', shade:'light'}
];
$scope.myColor = $scope.colors[2]; // red
$scope.colorsB = [
{name:'black', shade:'dark'},
{name:'white', shade:'light'},
{name:'red', shade:'dark'},
{name:'blue', shade:'dark'},
{name:'yellow', shade:'light'}
];
$scope.switchModel = function () {
$scope.colors = $scope.colorsB;
};
};
}]);
我遇到的问题是,在重新初始化数组$scope.myColor
后,如何选择$scope.colors
中包含的选项作为默认选项?
plunker代码在这里:http://plnkr.co/edit/50TVcrlxJHVH9kdEZljc?p=preview
答案 0 :(得分:0)
在 switchModel 功能中,再次更新模型如下。
$scope.switchModel = function () {
$scope.colors = $scope.colorsB;
$scope.myColor = $scope.colors[2];
};
感谢。
答案 1 :(得分:0)
$scope.switchModel = function () {
$scope.colors = $scope.colorsB;
//Reinit Your $scope.myColor
$scope.myColor = $scope.colors[2];
};