我是 angular 新手,尝试做一些指令嵌套,但遇到了一些问题。
这是我的代码:
module.controller("TimelineController", ["$scope", "$compile", function ($scope, $compile) {
$scope.text = "ohoh";
$scope.elements = ["12", "13"];
console.log("Timeline Controller", arguments);
}]);
module.directive('timeline', function() {
return {
restrict: 'E',
transclude: true,
scope: true,
controller : "TimelineController",
link: function(scope, element, attrs, controller) {
console.log("linking timeline", arguments);
},
templateUrl:'templates/directives/timeline.html',
replace: true
};
});
module.directive('timelineEvent', function() {
return {
restrict: 'E',
transclude: true,
scope: true,
// controller : "TimelineController",
link: function(scope, element, attrs/*, controller*/) {
console.log("linking element", arguments);
},
templateUrl:'templates/directives/timeline_element.html',
replace: false
};
});
我的模板:
时间线.html:
<div class="timeline">
<p>
timeline {{text}}
</p>
<div ng-repeat="element in elements">
- event {{element }}
<timeline-event ng-model="{{element}}"/>
</div>
</div>
时间线元素.html:
<div class="element">
timeline element o/ \o
</div>
我的 index.html :
[...]
<body>
<timeline></timeline>
</body>
和意想不到的结果:
timeline ohoh
- event 12
- event 13
timeline element o/ \o
预期的结果当然是:
timeline ohoh
- event 12
timeline element o/ \o
- event 13
timeline element o/ \o
为什么 ng-repeat 会成功执行,但嵌套指令调用只执行一次?它不应该能够在循环中使用指令吗?