日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 前端技术 > javascript >内容正文

javascript

AngularJS中使用ng-repeat的index

發布時間:2023/12/15 javascript 35 豆豆
生活随笔 收集整理的這篇文章主要介紹了 AngularJS中使用ng-repeat的index 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

AngularJS中的ng-repeat中,隱含的index,可以使用$index來訪問,也可以自己指定index對應的變量名。
- 使用隱含的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頁面內使用controller和定義的collection對象。

<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對象的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-popoverdata-template="partials/timeSlotPopover.html">{{datetimeSlots[dayIndex][timeIndex].displayStatus}} {{datetimeSlots[dayIndex][timeIndex].stdName}}</button>

ps:
$index是angular 內針對ng-repeat提供的隱含index變量名稱,如果在ng-repeat嵌套使用時,index名稱會發生沖突及覆蓋。這是也應該使用自定義的變量名。
下面例子中父級的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>

總結

以上是生活随笔為你收集整理的AngularJS中使用ng-repeat的index的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。