var module = angular.module('testApp', [])
.directive('onFinishRender', function ($timeout) {
return {
restrict: 'A',
link: function (scope, element, attr) {
if (scope.$last === true) {
$timeout(function () {
scope.$emit(attr.onFinishRender);
});
}
}
}
});
请注意,我没有使用.ready()
,而是将其包装在中$timeout
。$timeout
确保在ng重复元素真正完成渲染后$timeout
执行(因为将会在当前摘要周期的末尾执行-)。因此,ng- repeat
完成后,我们$emit
将向外部范围(同级和父范围)发出事件。
然后在您的控制器中,可以使用以下命令进行捕获$on
:
$scope.$on('ngRepeatFinished', function(ngRepeatFinishedEvent) {
//you also get the actual event object
//do stuff, execute functions -- whatever...
});
使用html看起来像这样:
<div ng-repeat="item in items" on-finish-render="ngRepeatFinished">
<div>{{item.name}}}<div>
</div>