0

我有一个稍微复杂的对象结构:

$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用另一个项目更新时,相应的数字会显示在下拉框中。但是当我从框中选择一个项目时, selectedItem 不会更新。但我注意到的是,我选择的所有选项都会在 DOM 中获得“选定”标签。

有什么建议吗?

4

1 回答 1

0

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>

于 2018-02-27T10:50:40.570 回答