AngularJS中使用ng-repeat的index問題
AngularJS使用ng-repeat的index
AngularJS中的ng-repeat中,隱含的index,可以使用$index來訪問,也可以自己指定index對(duì)應(yīng)的變量名。
使用隱含的index變量
隱含的index變量名是index,可以使用$index來訪問。
// 定義module和controller
var site = angular.module('application.site', []);
site.controller('MainController', ['$scope', '$http', function ($scope, $http) {
$scope.users = [
{name:"xialei",posts:["post一","post二","post三"]},
{name:"zhangsan",posts:["post四","post五"]}
];
}]);下面在html頁(yè)面內(nèi)使用controller和定義的collection對(duì)象。
<div ng-controller="MainController">
<dl ng-repeat="user in users">
<dt ng-init="p_index=$index">Name:{{ user.name }}</dt>
</dl>
</div>這里使用了$index,這是AngularJS提供的隱含的collection對(duì)象的index變量量。
指定index變量名
在ng-repeat中可以自己指定index的變量名稱,并在隨后使用。
比如下面代碼中,定義了times的index變量名稱timeIndex (為tr 元素), 為days的遍歷操作,定義了dayIndex的索引變量。
<tr data-ng-repeat="(timeIndex, time) in times">
<td style="text-align: center; width: 12.5%;" data-ng-style="doGetTimeColumnStyle($index)">{{time}}</td>
<td data-ng-repeat="(dayIndex, day) in days"
data-ng-click="selectDatetimeSlot(dayIndex, day, timeIndex, time)">
<button class="popupWindow"
data-ng-if="datetimeSlots[dayIndex][timeIndex].status && datetimeSlots[dayIndex][timeIndex].status != 'AVAILABLE' && datetimeSlots[dayIndex][timeIndex].status != 'EXPIRED' &&
datetimeSlots[dayIndex][timeIndex].mode != 'ONE_V_MANY'"
data-ng-class="datetimeSlots[dayIndex][timeIndex].displayStatus | lowercase"
data-placement="{{doGetTimeColumnPopOverPlacement(dayIndex, timeIndex)}}" data-animation="am-flip-x"
data-trigger="focus" data-bs-popover
data-template="partials/timeSlotPopover.html">
{{datetimeSlots[dayIndex][timeIndex].displayStatus}} {{datetimeSlots[dayIndex][timeIndex].stdName}}
</button>
ps:
$index是angular 內(nèi)針對(duì)ng-repeat提供的隱含index變量名稱,如果在ng-repeat嵌套使用時(shí),index名稱會(huì)發(fā)生沖突及覆蓋。這是也應(yīng)該使用自定義的變量名。
下面例子中父級(jí)的index使用ng-init,定義了p_index來指定為parent index。
<div ng-controller="MainController">
<dl ng-repeat="user in users">
<dt ng-init="p_index=$index">Name:{{ user.name }}</dt>
<dd ng-repeat="p in user.posts">Parent index:{{ p_index }} - {{ p }}
self Index:{{ $index }}
</dd>
</dl>
</div>
總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
詳解使用angular框架離線你的應(yīng)用(pwa指南)
這篇文章主要介紹了詳解使用angular框架離線你的應(yīng)用(pwa指南),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2019-01-01
AngularJS自定義插件實(shí)現(xiàn)網(wǎng)站用戶引導(dǎo)功能示例
這篇文章主要介紹了AngularJS自定義插件實(shí)現(xiàn)網(wǎng)站用戶引導(dǎo)功能,結(jié)合實(shí)例形式分析了AngularJS自定義插件的實(shí)現(xiàn)步驟與相關(guān)功能技巧,需要的朋友可以參考下2016-11-11
詳解AngularJS1.x學(xué)習(xí)directive 中‘& ’‘=’ ‘@’符號(hào)的區(qū)別使用
這篇文章主要介紹了詳解AngularJS1.x學(xué)習(xí)directive 中‘& ’‘=’ ‘@’符號(hào)的區(qū)別使用,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-08-08
Angular.js中用ng-repeat-start實(shí)現(xiàn)自定義顯示
大家都知道Angular.js可以用ng-repeat來顯示列表數(shù)據(jù),可是如果想要自定義顯示數(shù)據(jù)列表的話ng-repeat就實(shí)現(xiàn)不了了,這個(gè)時(shí)候可以利用ng-repeat-start 和 ng-repeat-end來實(shí)現(xiàn),下面通過本文來詳細(xì)看看實(shí)現(xiàn)的方法吧。2016-10-10
angularjs 的數(shù)據(jù)綁定實(shí)現(xiàn)原理
本篇文章主要介紹了angularjs 的數(shù)據(jù)綁定實(shí)現(xiàn)原理,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-07-07
深究AngularJS如何獲取input的焦點(diǎn)(自定義指令)
本篇文章主要介紹了AngularJS如何獲取input的焦點(diǎn)(自定義指令),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-06-06
AngularJS動(dòng)態(tài)加載模塊和依賴的方法分析
這篇文章主要介紹了AngularJS動(dòng)態(tài)加載模塊和依賴的方法,結(jié)合實(shí)例形式分析了AngularJS使用ocLazyLoad實(shí)現(xiàn)動(dòng)態(tài)加載的相關(guān)操作技巧,需要的朋友可以參考下2016-11-11
詳解Angular5 服務(wù)端渲染實(shí)戰(zhàn)
本篇文章主要介紹了詳解Angular5 服務(wù)端渲染實(shí)戰(zhàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
Angular5中狀態(tài)管理的實(shí)現(xiàn)
這篇文章主要介紹了Angular5中狀態(tài)管理的實(shí)現(xiàn),小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-09-09

