我想调用一些jQuery函数针对div表。该表由ng-repeat填充。
当我打开它的时候
$(document).ready()
我没有结果。
也
$scope.$on('$viewContentLoaded', myFunc);
没有帮助。
是否有办法在ng-repeat填充完成后立即执行函数?我读了一个关于使用自定义指令的建议,但我不知道如何使用ng-repeat和我的div…
我想调用一些jQuery函数针对div表。该表由ng-repeat填充。
当我打开它的时候
$(document).ready()
我没有结果。
也
$scope.$on('$viewContentLoaded', myFunc);
没有帮助。
是否有办法在ng-repeat填充完成后立即执行函数?我读了一个关于使用自定义指令的建议,但我不知道如何使用ng-repeat和我的div…
当前回答
我在这里找到了一个很好的答案,但仍然有必要增加一个延迟
创建以下指令:
angular.module('MyApp').directive('emitLastRepeaterElement', function() {
return function(scope) {
if (scope.$last){
scope.$emit('LastRepeaterElement');
}
}; });
将它作为一个属性添加到你的中继器中,像这样:
<div ng-repeat="item in items" emit-last-repeater-element></div>
根据Radu的说法:
$scope.eventoSelecionado.internamento_evolucoes.forEach(ie => {mycode});
对我来说,它是有效的,但我仍然需要添加一个setTimeout
$scope.eventoSelecionado.internamento_evolucoes.forEach(ie => {
setTimeout(function() {
mycode
}, 100); });
其他回答
实际上,您应该使用指令,并且ng-Repeat循环的末尾没有绑定事件(因为每个元素都是单独构造的,并且有自己的事件)。但是a)使用指令可能是你所需要的,b)有一些ng-Repeat特定的属性,你可以使用它来创建你的“on ngRepeat finished”事件。
具体来说,如果你想要的只是在整个表中添加事件样式,你可以使用包含所有ngRepeat元素的指令来实现。另一方面,如果你想具体地处理每个元素,你可以在ngRepeat中使用一个指令,它将在每个元素被创建后作用于每个元素。
然后,还有可以用来触发事件的$index、$first、$middle和$last属性。对于这个HTML:
<div ng-controller="Ctrl" my-main-directive>
<div ng-repeat="thing in things" my-repeat-directive>
thing {{thing}}
</div>
</div>
你可以像这样使用指令:
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');
};
});
看看这款Plunker的应用吧。
与Pavel的回答相辅相成的是:
<ul>
<li ng-repeat="item in items"
ng-init="$last ? doSomething() : angular.noop()">{{item}}</li>
</ul>
不然你为什么会觉得棱角分明。不,一开始就有吗?
优点:
你不需要为此写一个指令……
我在这里找到了一个很好的答案,但仍然有必要增加一个延迟
创建以下指令:
angular.module('MyApp').directive('emitLastRepeaterElement', function() {
return function(scope) {
if (scope.$last){
scope.$emit('LastRepeaterElement');
}
}; });
将它作为一个属性添加到你的中继器中,像这样:
<div ng-repeat="item in items" emit-last-repeater-element></div>
根据Radu的说法:
$scope.eventoSelecionado.internamento_evolucoes.forEach(ie => {mycode});
对我来说,它是有效的,但我仍然需要添加一个setTimeout
$scope.eventoSelecionado.internamento_evolucoes.forEach(ie => {
setTimeout(function() {
mycode
}, 100); });
我必须在ng-repeat结束后使用MathJax渲染公式,上面的答案都没有解决我的问题,所以我做了如下。这不是一个很好的解决方案,但对我来说很有效。
<div ng-repeat="formula in controller.formulas">
<div>{{formula.string}}</div>
{{$last ? controller.render_formulas() : ""}}
</div>
<div ng-repeat="i in items">
<label>{{i.Name}}</label>
<div ng-if="$last" ng-init="ngRepeatFinished()"></div>
</div>
我的解决方案是添加一个div来调用一个函数,如果项目是repeat中的最后一个。