你如何渲染后执行的指令,code? [英] How do you execute directive code after rendering?
问题描述
我试图构建一个嵌套的 NG-重复
已完成渲染之后运行一个指令。以下是我已经试过(小提琴):
I'm trying to build a directive that runs after a nested ng-repeat
has completed rendering. Here's what I've tried (fiddle):
中的HTML:
<div ng-app="MyApp" ng-controller="MyCtrl">
<ul my-directive>
<li ng-repeat="animal in animals">{{animal}}</li>
</ul>
</div>
和JavaScript的:
And the JavaScript:
angular.module("MyApp", [])
.directive("myDirective", function () {
return function(scope, element, attrs) {
alert(element.find("li").length); // 0
};
});
function MyCtrl($scope) {
$scope.animals = ["Dog", "Cat", "Elephant"];
}
李&GT; 元素都被渲染(和 在我的自定义指令中的链接功能全部 The linking function in my custom directive runs before all of the 您可以移动NG-重复里面的指令,的 http://jsfiddle.net/ZTMex/3/ You could move the directive inside ng-repeat, http://jsfiddle.net/ZTMex/3/ 或者看看 http://stackoverflow.com/a/13472605/1986890 这是关系到你的问题。 这篇关于你如何渲染后执行的指令,code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋! 0 $ C $
&LT之前运行C>被警告)。我如何运行
之后,code NG-重复
已完成渲染?<li>
elements have been rendered (and 0
is alerted). How do I run code after the ng-repeat
has completed rendering?推荐答案
<div ng-app="MyApp" ng-controller="MyCtrl">
<ul>
<li ng-repeat="animal in animals" my-directive>{{animal}}</li>
</ul>
</div>