當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
AngularJS中使用ng-repeat的index
生活随笔
收集整理的這篇文章主要介紹了
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的索引變量。
ps:
$index是angular 內針對ng-repeat提供的隱含index變量名稱,如果在ng-repeat嵌套使用時,index名稱會發生沖突及覆蓋。這是也應該使用自定義的變量名。
下面例子中父級的index使用ng-init,定義了p_index來指定為parent index。
總結
以上是生活随笔為你收集整理的AngularJS中使用ng-repeat的index的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 现金价值与减额交清保额是什么意思
- 下一篇: 17岁怎么贷款急用钱