[英]angular set option value as array iteration with ng-select
From the controller: 从控制器:
$scope.form_data = {
day: 'Day'
};
$scope.days = [
'Day',1,2,3,4,5,6,7,8,9,10,
11,12,13,14,15,16,17,18,19,20,
21,22,23,24,25,26,27,28,29,30,
31
];
From the html: 从html:
<select ng-model="form_data.day" ng-options="day for day in days"></select>
The results is: 结果是:
<select ng-options="day as day for day in days" ng-model="form_data.day" class="ng-pristine ng-valid ng-touched">
<option value="string:Day" label="Day" selected="selected">Day</option>
<option value="number:1" label="1">1</option>
<option value="number:2" label="2">2</option>
....
</select>
Which is great.. but I cannot figure out from the docs how to set the value of the option as the array key, ie from the above example, the value of the first option i need to be 0, ie the first pos. 太好了..但是我不能从文档中找出如何将选项的值设置为数组键,即从上面的示例中,我需要将第一个选项的值设置为0,即第一个pos。 in the array.
在数组中。
Is this possible with ng-options? ng-options是否可能?
Yes it's possible, you were almost there. 是的,有可能,您快到了。
See the first response of this post: Put an index to a model with ng-options for a simple array 请参阅本文的第一个回复: 使用ng-options为简单数组添加模型索引
Your select balise becomes: 您选择的balise变为:
<select
ng-model="form_data.day"
ng-options="days.indexOf(day) as day for day in days"></select>
I wrote an example : https://jsfiddle.net/w7jdktxn/1/ 我写了一个例子: https : //jsfiddle.net/w7jdktxn/1/
never did that, but that is what you asked ! 从来没有那样做,但这就是你的要求!
<div ng-init="days = [1,2,3,4,5]">
<select ng-model="form_data.day" class="ng-pristine ng-valid ng-touched">
<option value="" disabled selected style="display: none;">Select day</option>
<option ng-repeat="day in days" value="{{$index}}" >{{day}}</option>
</select>
<h3>form_data.day={{form_data.day}}</h3>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.