在角度下拉菜单中显示选定值时遇到问题。 它工作时,我给这样的AngularJS下拉菜单不显示选定的值
$scope.selectedItem = $scope.items[1];
不工作,如果我直接给该值
$scope.selectedItem = { name: 'two', age: 27 };
HTML:
<html ng-app="app">
<body>
<div ng-controller="Test">
<select ng-model="selectedItem" ng-options="item.name for item in items">
</select>
</div>
</body>
</html>
JS:
var app = angular.module('app',[]);
app.controller('Test',function($scope){
$scope.items = [{name: 'one', age: 30 },{ name: 'two', age: 27 },{ name: 'three', age: 50 }];
$scope.selectedItem = $scope.items[1];
});
CODEPEN: http://codepen.io/anon/pen/zxXpmR
SOLUTION:
谢谢萨米尔-DAS。我根据你的建议进行了修正。
var choosen_value = { name: 'two', age: 27 };
angular.forEach($scope.items, function(item){
if(angular.equals(choosen_value, item)){
$scope.selectedItem = item;
}
});
你有什么办法解决这一问题? – 2015-04-02 07:52:43
你将不得不使用同一个对象。您可以首先搜索您想要开始的项目,然后分配找到的项目 – Samir 2015-04-02 08:04:01