AngularJS ng-option获取索引 [英] AngularJS ng-option get index
问题描述
我正在研究的项目中使用播放列表选项,并且试图获取列表中所有视频的下拉列表.
I'm making a playlist option in the project I'm working on and I'm trying to get a dropdown for all the videos in the list.
这很容易
<select ng-options="vid.Id for vid in playList.Videos track by video.Id" ng-model="selected" class="browser-default"></select>
但是这显示了Id的下拉列表,但是现在我希望它在列表+1中显示位置;
but this shows a drop down of the Id's, but now I want it to show the position in the list +1;
因此,在播放视频5/15时,下拉菜单应显示1到15(而不是0到14),并且应选择5.
So when playing video 5/15 the drop down should show 1 to 15 (so not 0 to 14) and 5 should be selected.
关于如何完成此操作的任何想法?
any idea on how to accomplish this?
推荐答案
如果要将当前视频的索引显示为选项文本,则此ngOptions表达式应该可以解决问题:
If you want to display index of the current video as option text, then this ngOptions expression should do the trick:
ng-options="vid as playList.Videos.indexOf(vid)+1 for vid in playList.Videos track by video.Id"
此处的关键是您可以使用 1
.
The key here is that you can use indexOf
method on Videos
array to calculate current video index, and then add 1
.
这篇关于AngularJS ng-option获取索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!