gpt4 book ai didi

angularjs - 为 ng-options 绑定(bind)选择元素设置选定值

转载 作者:行者123 更新时间:2023-12-03 08:31:10 25 4
gpt4 key购买 nike

摆弄相关代码:http://jsfiddle.net/gFCzV/7/

我正在尝试设置绑定(bind)到 ng-repeat 中引用的对象的子集合的下拉列表的选定值。我不知道如何设置选定的选项,因为我无法以我知道的任何方式引用它所绑定(bind)的集合。

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",ID:1}, {Name:"PB&J",ID:2}, {Name:"Fish",ID:3}],
"SelectedOption":""
}
],
}, {
"firstName": "Jane",
"lastName": "Doe"
}];
});

这是我应该在模型上实际使用带有 SelectedIndex 的 ng-init 的一种情况吗?

最佳答案

如果使用 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>

更新 fiddle http://jsfiddle.net/gFCzV/34/

关于angularjs - 为 ng-options 绑定(bind)选择元素设置选定值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20845210/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com