小提示相关代码:
http://jsfiddle.net/gFCzV/7/
我试图设置一个下拉的选定的值绑定到一个对象的子集合在一个ng-repeat引用。我不知道如何设置所选的选项,因为我不能以任何我知道的方式引用绑定的集合。
HTML:
<div ng-app="myApp" ng-controller="SomeController"> <div ng-repeat="Person in People"> <div class="listheader">{{Person.firstName}} {{Person.lastName}}</div> <div class="listitem" ng-repeat="Choice in Person.Choices"> {{Choice.Name}}: <select ng-model="Choice.SelectedOption" ng-options="choice.Name for choice in Choice.Options"></select> {{Choice.SelectedOption.ID}} </div> </div> </div>
JS:
var myApp = angular.module('myApp',[]); myApp.controller("SomeController",function($scope) { $scope.People = [{ "firstName": "John","lastName": "Doe","Choices": [ { "Name":"Dinner","Options":[{Name:"Fish",ID:1},{Name:"Chicken",ID:2},{Name:"Beef",ID:3}],"SelectedOption":{Name:"Chicken",ID:2} //this doesn't work },{ "Name":"Lunch","Options":[{Name:"Macaroni",{Name:"PB&J",{Name:"Fish","SelectedOption":"" } ],},{ "firstName": "Jane","lastName": "Doe" }]; });
这是一个case,我实际上应该使用ng-init模型上的SelectedIndex?
如果使用AngularJS 1.2,您可以使用“track by”来告诉Angular如何比较对象。
原文链接:https://www.f2er.com/angularjs/145145.html<select ng-model="Choice.SelectedOption" ng-options="choice.Name for choice in Choice.Options track by choice.ID"> </select>