最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - AngularJS ng-option get index - Stack Overflow

programmeradmin2浏览0评论

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.

which is easy

<select ng-options="vid.Id for vid in playList.Videos track by video.Id" ng-model="selected" class="browser-default"></select>

but this shows a drop down of the Id's, but now I want it to show the position in the list +1;

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 acplish this?

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.

which is easy

<select ng-options="vid.Id for vid in playList.Videos track by video.Id" ng-model="selected" class="browser-default"></select>

but this shows a drop down of the Id's, but now I want it to show the position in the list +1;

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 acplish this?

Share Improve this question asked Mar 4, 2015 at 7:50 KiwiKiwi 2,7737 gold badges49 silver badges86 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 7

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"

The key here is that you can use indexOf method on Videos array to calculate current video index, and then add 1.

You can track by the index of the array. $index is set to the item index or key.

<select ng-options="vid as ($index + 1) for vid in playList.Videos track by $index" ng-model="selected" class="browser-default"></select>
发布评论

评论列表(0)

  1. 暂无评论