angularjs – 为ng选项绑定选择元素设置所选值

前端之家收集整理的这篇文章主要介绍了angularjs – 为ng选项绑定选择元素设置所选值前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
提示相关代码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如何比较对象。
<select 
    ng-model="Choice.SelectedOption"                 
    ng-options="choice.Name for choice in Choice.Options track by choice.ID">
</select>

更新小提琴http://jsfiddle.net/gFCzV/34/

原文链接:https://www.f2er.com/angularjs/145145.html

猜你在找的Angularjs相关文章