這是我如何解決它:
HTML:
<!doctype html>
<html ng-app="myApp">
<head>
<meta charset="utf-8">
<title>AngularJS Plunker</title>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<link rel="stylesheet" href="style.css">
<script data-require="[email protected]" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.6/angular.js"></script>
<script src="app.js"></script>
</head>
<body>
<div ng-controller="MainCtrl">
<outer>
<inner ng-repeat="d in data">
<div>{{d}}</div>
</inner>
</outer>
</div>
</body>
</html>
App.js:
var myApp = angular.module('myApp', ['myApp.directives']);
myApp.controller('MainCtrl', function($scope) {
$scope.data = [1, 2, 3];
});
var directives = angular.module('myApp.directives', []);
directives.directive('inner', function() {
return {
restrict: 'E',
scope: true,
link: function(scope, element, attrs) {
element.append("<div>Processing inner element.</div>");
if (scope.$last) {
element.append("Processed last inner element.");
scope.$emit('foo', "");
}
}
}
});
directives.directive('outer', function() {
return {
restrict: 'E',
scope: true,
link: function(scope, element, attrs) {
scope.$on('foo', function() {
element.append("<div>Finished outer processing.</div>");
element.append(element.getChildren());
// this is critical if jQuery altered DOM in inner directive
scope.$apply();
});
}
}
});
輸出:
1
Processing inner element.
2
Processing inner element.
3
Processing inner element.
Processed last inner element.
Finished outer processing.
Plunker:http://plnkr.co/edit/tMXq7fFeNLnDbn9ORdUS
@Chandermani - 謝謝你的推動!
This SO post would help you http://stackoverflow.com/questions/15207788/calling-a-function-when-ng-repeat-has-finished – Chandermani
@Chandermani - 謝謝。我嘗試添加到內部指令:if(scope。$ last === true){$ timeout(function(){scope.finished = true;});},然後從外部指令中觀察。範圍發生變化,但觸發第一個元素而非最後一個元素。 –
你不應該在你的子指令中做'scope.finished = true;'。這設置了本地範圍。您應該使用'$ scope。$ emit'來引發一個事件。 – Chandermani