我想调用一些jQuery函数针对div表。该表由ng-repeat填充。

当我打开它的时候

$(document).ready()

我没有结果。

$scope.$on('$viewContentLoaded', myFunc);

没有帮助。

是否有办法在ng-repeat填充完成后立即执行函数?我读了一个关于使用自定义指令的建议,但我不知道如何使用ng-repeat和我的div…


当前回答

与Pavel的回答相辅相成的是:

<ul>
    <li ng-repeat="item in items" 
        ng-init="$last ? doSomething() : angular.noop()">{{item}}</li>
</ul>

不然你为什么会觉得棱角分明。不,一开始就有吗?

优点:

你不需要为此写一个指令……

其他回答

当你检查范围时,这也是必要的。用setTimeout(someFn, 0)来包装你的触发器。setTimeout 0是javascript中被接受的技术,它是我的指令正确运行的必要条件。

这里是一个重复完成指令,当为真时调用指定的函数。我发现被调用的函数在做DOM操作之前必须使用interval=0的$timeout,比如在呈现的元素上初始化工具提示。jsFiddle: http://jsfiddle.net/tQw6w/

在美元范围。layoutDone,试着注释掉$timeout行和取消注释“NOT CORRECT!”行,看看工具提示中的区别。

<ul>
    <li ng-repeat="feed in feedList" repeat-done="layoutDone()" ng-cloak>
    <a href="{{feed}}" title="view at {{feed | hostName}}" data-toggle="tooltip">{{feed | strip_http}}</a>
    </li>
</ul>

JS:

angular.module('Repeat_Demo', [])

    .directive('repeatDone', function() {
        return function(scope, element, attrs) {
            if (scope.$last) { // all are rendered
                scope.$eval(attrs.repeatDone);
            }
        }
    })

    .filter('strip_http', function() {
        return function(str) {
            var http = "http://";
            return (str.indexOf(http) == 0) ? str.substr(http.length) : str;
        }
    })

    .filter('hostName', function() {
        return function(str) {
            var urlParser = document.createElement('a');
            urlParser.href = str;
            return urlParser.hostname;
        }
    })

    .controller('AppCtrl', function($scope, $timeout) {

        $scope.feedList = [
            'http://feeds.feedburner.com/TEDTalks_video',
            'http://feeds.nationalgeographic.com/ng/photography/photo-of-the-day/',
            'http://sfbay.craigslist.org/eng/index.rss',
            'http://www.slate.com/blogs/trending.fulltext.all.10.rss',
            'http://feeds.current.com/homepage/en_US.rss',
            'http://feeds.current.com/items/popular.rss',
            'http://www.nytimes.com/services/xml/rss/nyt/HomePage.xml'
        ];

        $scope.layoutDone = function() {
            //$('a[data-toggle="tooltip"]').tooltip(); // NOT CORRECT!
            $timeout(function() { $('a[data-toggle="tooltip"]').tooltip(); }, 0); // wait...
        }

    })

没有必要创建一个指令,特别是为了有一个ng-repeat完成事件。

Ng-init会为您带来魔力。

  <div ng-repeat="thing in things" ng-init="$last && finished()">

$last确保,当最后一个元素被呈现给DOM时,finished才会被触发。

不要忘记创建$scope。完成事件。

编码快乐! !

编辑:2016年10月23日

如果你还想在数组中没有项的情况下调用完成的函数,那么你可以使用下面的解决方法

<div style="display:none" ng-init="things.length < 1 && finished()"></div>
//or
<div ng-if="things.length > 0" ng-init="finished()"></div>

只需在ng-repeat元素的顶部添加上面的行。它将检查数组是否没有任何值,并相应地调用函数。

E.g.

<div ng-if="things.length > 0" ng-init="finished()"></div>
<div ng-repeat="thing in things" ng-init="$last && finished()">

我是这样做的。

创建指令

function finRepeat() {
    return function(scope, element, attrs) {
        if (scope.$last){
            // Here is where already executes the jquery
            $(document).ready(function(){
                $('.materialboxed').materialbox();
                $('.tooltipped').tooltip({delay: 50});
            });
        }
    }
}

angular
    .module("app")
    .directive("finRepeat", finRepeat);

在你把它添加到这个ng-repeat的标签上之后

<ul>
    <li ng-repeat="(key, value) in data" fin-repeat> {{ value }} </li>
</ul>

然后在ng-repeat结束时运行。

这是对其他回答中表达的想法的改进,以展示如何在使用声明性语法和使用element-directive隔离作用域(谷歌推荐的最佳实践)时访问ngRepeat属性($index, $first, $middle, $last, $even, $odd)。注意主要的区别:scope.$parent.$last。

angular.module('myApp', [])
.directive('myRepeatDirective', function() {
  return {
    restrict: 'E',
    scope: {
      someAttr: '='
    },
    link: function(scope, element, attrs) {
      angular.element(element).css('color','blue');
      if (scope.$parent.$last){
        window.alert("im the last!");
      }
    }
  };
});