执行ng重复完成功能

问题描述:

我有一个ng-repeat的div,当我试图在加载时访问这些div时,它们还不存在。如何在ng-repeat完成时执行一个函数?我试过指令,但他们似乎没有工作。执行ng重复完成功能

+0

检查'ng-repeat'循环中'$ last'属性。当它被设置,你可以执行功能 – mohamedrias 2015-04-03 07:22:11

+4

可能重复http://*.com/questions/15207788/calling-a-function-when-ng-repeat-has-finished – squiroid 2015-04-03 07:22:42

+0

@squiroid我试过这种方法,它didn没有工作。*里亚斯,我会怎么做?你能举个例子吗? – Zygimantas 2015-04-03 07:26:12

如果可能的重复不满足您的需求,则可以使用这种方法 - Plunker

JS

app = angular.module("app", []); 

app.directive("test", [ function() { 
    return { 
     restrict: "E", 
     template: "<div ng-repeat='item in ts.items track by $index' ng-init='ts.blah($index, $last)'>{{$index}}</div>", 
     scope: {}, 
     controllerAs: "ts", 
     bindToController: true, 
     controller:function() { 
      var ts = this; 
      ts.items = [0, 1, 2, 3, 4]; 
      ts.blah = function(index, last) { 
      console.log(index, last); 
      if (last) { 
       // Do your stuff 
      } 
      } 
     } 
    } 
}]); 

标记

<!DOCTYPE html> 
<html> 

    <head> 
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.15/angular.min.js"></script> 
    <link rel="stylesheet" href="style.css"> 
    <script src="script.js"></script> 
    </head> 

    <body ng-app="app"> 
    <test></test> 
    </body> 

</html> 

从控制台输出:

0假
1假
2假
3假
4真

所以你应该(理论上)能够依靠最后创建的最后一个重复元素。