你如何渲染后执行的指令,code? [英] How do you execute directive code after rendering?

查看:148
本文介绍了你如何渲染后执行的指令,code?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图构建一个嵌套的 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; 元素都被渲染(和 0 在我的自定义指令中的链接功能全部&LT之前运行C>被警告)。我如何运行之后,code NG-重复已完成渲染?

The linking function in my custom directive runs before all of the <li> elements have been rendered (and 0 is alerted). How do I run code after the ng-repeat has completed rendering?

推荐答案

您可以移动NG-重复里面的指令,的 http://jsfiddle.net/ZTMex/3/

You could move the directive inside ng-repeat, http://jsfiddle.net/ZTMex/3/

<div ng-app="MyApp" ng-controller="MyCtrl">
    <ul>
        <li ng-repeat="animal in animals" my-directive>{{animal}}</li>
    </ul>
</div>

或者看看 http://stackoverflow.com/a/13472605/1986890 这是关系到你的问题。

这篇关于你如何渲染后执行的指令,code?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆