需要AngularJS输入表单例如选择用法 [英] Need example of select usage in AngularJS input form
问题描述
我在尝试使用对象的数组
I am attempting to use an array of objects
(array = [{name: 'Moe', id:1}, {name: 'Larry', id:2}, {name: 'Curly', id:3}])
作为AngularJS输入表单中的下拉选择,我需要一个例子厉害。任何人都可以点我到的jsfiddle,可以告诉我吗?
as a drop-down selection within an AngularJS input form, and I need an example badly. Can anyone point me to a jsFiddle that can enlighten me?
推荐答案
我会建议使用NG-选择,您可以。
I would recommend using ng-options where you can.
<select ng-model="choice" ng-options="x.id as x.name for x in array"></select>
由于检查了这一点:你可以用它来选择的实际对象的:
Because check this out: You can use it to select actual objects:
app.controller('ExampleCtrl', function($scope) {
$scope.items = [
{ id: 1, name: 'Foo' },
{ id: 2, name: 'Bar' }
];
$scope.selectedItem = null;
});
<div ng-controller="ExampleCtrl">
<select ng-model="selectedItem"
ng-options="item as item.name for item in items"></select>
{{selectedItem | json}}
</div>
在当您选择从下拉的东西上面的例子中,它实际上是在设置一个将selectedItem整个对象引用,而只是一个值类型。
In the above example when you select something from the drop down, it's actually setting an entire object reference on selectedItem rather that just a value type.
注:使用NG选项设置你的值=
属性在您的收藏项目的索引的这是由设计的。
NOTE: using ng-options sets your value=""
attributes to the indices of the items in your collections this is by design.
这是真正做到这一点的最好办法。
It's really the best way to do it.
编辑:根据要求更多的上下文。此外这里是展示在使用它plunker
more context as requested. Also here's a plunker showing it in use
这篇关于需要AngularJS输入表单例如选择用法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!