3

ng-repeatネストされたレンダリングが完了した後に実行されるディレクティブを構築しようとしています。これが私が試したことです(フィドル)

HTML:

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

そして 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"];
}

カスタム ディレクティブのリンク関数は、すべての<li>要素がレンダリングされる前に実行されます (そして0警告が表示されます)。ng-repeatレンダリングが完了した後にコードを実行するにはどうすればよいですか?

4

1 に答える 1

3

ディレクティブを 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>

または、質問に関連するhttps://stackoverflow.com/a/13472605/1986890をご覧ください。

于 2013-01-26T23:10:15.050 に答える