执行ng重复完成功能

时间:2015-04-03 07:19:51

标签: javascript angularjs html5

我有一个重复的div,当我尝试在加载时访问这些div时,它们还没有存在。 ng-repeat结束后如何执行功能?我试过指令,但它们似乎不起作用。

1 个答案:

答案 0 :(得分:1)

如果可能的副本不能满足您的需求,可以采用这种方法 - 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 false
1假
2假1 3假4真

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

相关问题