设置选定值NG选项绑定选择元素

编程入门 行业动态 更新时间:2024-10-27 12:34:10
本文介绍了设置选定值NG选项绑定选择元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

拨弄相关code: jsfiddle/gFCzV/7/

我想设置一个下降的设定值下降,则势必要在NG-重复引用的对象的子集。我不知道如何选择的选项设置,因为我不能引用它被绑定到任何方式,我所知道的集合。

I'm trying to set the selected value of a drop down that is bound to a child collection of an object referenced in an ng-repeat. I don't know how to set the selected option since I can't reference the collection it's being bound to in any way that I'm aware of.

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" }]; });

这是一个情况下,我应该实际使用NG-INIT与型号?

Is this the one case where I should actually be using ng-init with a SelectedIndex on the model?

推荐答案

如果使用AngularJS 1.2可以按曲目用它来告诉角度如何比较的对象。

If using AngularJS 1.2 you can use 'track by' to tell Angular how to compare objects.

<select ng-model="Choice.SelectedOption" ng-options="choice.Name for choice in Choice.Options track by choice.ID"> </select>

更新小提琴 jsfiddle/gFCzV/34/

更多推荐

设置选定值NG选项绑定选择元素

本文发布于:2023-11-06 08:04:16,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1563138.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:绑定   选项   元素   NG

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!