angularjs如何从项目模板中访问ng-repeat数组

时间:2012-12-15 22:37:52

标签: angularjs

我想这样做......

<span ng-repeat="item in array1" ng-include="'genericItemTemplate.html'">

这......

<span ng-repeat="item in array2" ng-include="'genericItemTemplate.html'">

然后在genericItemTemplate中......执行此操作...

[{{$index + 1}} of {{someArrayreference.length}}]

我可以在模板中引用ng-repeat的数组吗?

另见我的帖子...... https://groups.google.com/forum/?fromgroups=#!topic/angular/y_KuX0HRQ_U

和我的plunkr

http://plnkr.co/edit/dY7M76kPyLeK0fvVZhvB

迈克尔

1 个答案:

答案 0 :(得分:4)

使用onload,ng-include可选参数:

<li ng-repeat="person in personlist=friends" ng-include="'myGenericTemplate.html'"
    onload="len=friends.length"></li>

<li ng-repeat="person in personlist=collegues" ng-include="'myGenericTemplate.html'"
    onload="len=collegues.length"></li>

[{{$index + 1}} of {{len}}] {{person.name}} 

输出:

I have friends. They are:
[1 of 2] John a boy who is 25 years old.
[2 of 2] Mary a girl

I also have collegues. They are:
[1 of 3] Hoolio a boy who is 42 years old.
[2 of 3] Kitty a girl
[3 of 3] Marge a girl