在 ngOptions 中选择一个选项不会更新模型
Selecting an option in ngOptions doesn't update the model
我有一个稍微复杂的对象结构:
$scope.items =
[
{
name: 'something',
complexObject:
{
number: 1,
id: 12345
}
},
…more of those
]
$scope.selectedItem = $scope.items[0];
现在我想创建一个这样的下拉框:
<select
ng-options="item as item.complexObject.number for item in items track by items.complexObject.id"
ng-model="selectedItem">
当我用另一个项目更新 selectedItem
时,相应的数字显示在下拉框中。但是当我 select 盒子里的一个项目时 selectedItem 没有更新。不过我注意到的是,我选择的所有选项都在 DOM.
中获得了 'selected' 标签
有什么建议吗?
var app = angular.module("Profile", [] );
app.controller("ProfileCtrl", function($scope) {
$scope.items =
[
{
name: 'something',
complexObject:
{
number: 1,
id: 12345
}
},
{
name: 'something',
complexObject:
{
number: 2,
id: 12345
}
},
]
$scope.selectedItem = {}
$scope.selectedItem['val'] = $scope.items[0];
$scope.get_info = function(){
alert('Selected Row number "'+JSON.stringify($scope.selectedItem['val'])+'"')
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Profile" ng-controller="ProfileCtrl">
<select ng-options="item as item.complexObject.number for item in items" ng-model="selectedItem['val']"></select>
<button class="btn" ng-click="get_info()">Click and see whick item selected</button>
</body>
我有一个稍微复杂的对象结构:
$scope.items =
[
{
name: 'something',
complexObject:
{
number: 1,
id: 12345
}
},
…more of those
]
$scope.selectedItem = $scope.items[0];
现在我想创建一个这样的下拉框:
<select
ng-options="item as item.complexObject.number for item in items track by items.complexObject.id"
ng-model="selectedItem">
当我用另一个项目更新 selectedItem
时,相应的数字显示在下拉框中。但是当我 select 盒子里的一个项目时 selectedItem 没有更新。不过我注意到的是,我选择的所有选项都在 DOM.
有什么建议吗?
var app = angular.module("Profile", [] );
app.controller("ProfileCtrl", function($scope) {
$scope.items =
[
{
name: 'something',
complexObject:
{
number: 1,
id: 12345
}
},
{
name: 'something',
complexObject:
{
number: 2,
id: 12345
}
},
]
$scope.selectedItem = {}
$scope.selectedItem['val'] = $scope.items[0];
$scope.get_info = function(){
alert('Selected Row number "'+JSON.stringify($scope.selectedItem['val'])+'"')
}
})
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="Profile" ng-controller="ProfileCtrl">
<select ng-options="item as item.complexObject.number for item in items" ng-model="selectedItem['val']"></select>
<button class="btn" ng-click="get_info()">Click and see whick item selected</button>
</body>