这是我解决它的方法:
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="angular.js@1.0.x" 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>
应用程序.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 - 感谢您的推动!