ng-重复完成事件 [英] ng-repeat finish event
问题描述
我想用表格调用一些针对 div 的 jQuery 函数.该表填充了 ng-repeat
.
当我调用它时
$(document).ready()
我没有结果.
还有
$scope.$on('$viewContentLoaded', myFunc);
没有帮助.
ng-repeat 填充完成后有什么方法可以立即执行函数吗?我已经阅读了关于使用自定义 directive
的建议,但我不知道如何将它与 ng-repeat 和我的 div 一起使用...
确实,您应该使用指令,并且没有与 ng-Repeat 循环结束相关的事件(因为每个元素都是单独构造的,并且具有自己的事件).但是 a) 使用指令可能就是你所需要的,b) 有一些 ng-Repeat 特定属性可以用来制作on ngRepeat 完成"事件.
具体来说,如果您只想为整个表格设置样式/添加事件,您可以在包含所有 ngRepeat 元素的指令中使用.另一方面,如果你想具体地处理每个元素,你可以在 ngRepeat 中使用一个指令,它会在每个元素被创建后作用.
然后,您可以使用 $index
、$first
、$middle
和 $last
属性用于触发事件.所以对于这个 HTML:
<div ng-repeat="事物中的事物" my-repeat-directive>东西{{东西}}
你可以像这样使用指令:
angular.module('myApp', []).directive('myRepeatDirective', function() {返回函数(范围,元素,属性){angular.element(element).css('color','blue');如果(范围.$last){window.alert("我是最后一个!");}};}).directive('myMainDirective', function() {返回函数(范围,元素,属性){angular.element(element).css('border','5px 纯红色');};});
在这个 Plunker 中查看它的实际效果.希望能帮到你!
I want to call some jQuery function targeting div with table. That table is populated with ng-repeat
.
When I call it on
$(document).ready()
I have no result.
Also
$scope.$on('$viewContentLoaded', myFunc);
doesn't help.
Is there any way to execute function right after ng-repeat population completes? I've read an advice about using custom directive
, but I have no clue how to use it with ng-repeat and my div...
Indeed, you should use directives, and there is no event tied to the end of a ng-Repeat loop (as each element is constructed individually, and has it's own event). But a) using directives might be all you need and b) there are a few ng-Repeat specific properties you can use to make your "on ngRepeat finished" event.
Specifically, if all you want is to style/add events to the whole of the table, you can do so using in a directive that encompasses all the ngRepeat elements. On the other hand, if you want to address each element specifically, you can use a directive within the ngRepeat, and it will act on each element, after it is created.
Then, there are the $index
, $first
, $middle
and $last
properties you can use to trigger events. So for this HTML:
<div ng-controller="Ctrl" my-main-directive>
<div ng-repeat="thing in things" my-repeat-directive>
thing {{thing}}
</div>
</div>
You can use directives like so:
angular.module('myApp', [])
.directive('myRepeatDirective', function() {
return function(scope, element, attrs) {
angular.element(element).css('color','blue');
if (scope.$last){
window.alert("im the last!");
}
};
})
.directive('myMainDirective', function() {
return function(scope, element, attrs) {
angular.element(element).css('border','5px solid red');
};
});
See it in action in this Plunker. Hope it helps!
这篇关于ng-重复完成事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!