我想在ngrepeat中动态地添加指令。请参考以下链接小提琴:Fiddle Link在ngrepeat中动态添加指令时出现奇怪的行为
代码:
// Code goes here
var app = angular.module('myApp',[]);
// myDir Directive
app.directive('myDir', function() {
var controller = ['$scope','$compile', function ($scope,$compile) {
$scope.names=[{id:'1',directive:'subDir1'},{id:'2',directive:'subDir2'}];
$scope.loadDynamicDir = function(id, directive) {
var newScope = $scope.$new(true);
var html = $compile('<div class="' + directive + '"></div>')(newScope);
angular.element(document.getElementById('div' + id)).append(html);
}
}]
return {
controller:controller,
templateUrl:'myDirTemplate.html'
}
})
// subDir1 Directive
app.directive('subDir1', function() {
return {
restrict:'C',
template: 'subDir1'
}
});
// subDir2 Directive
app.directive('subDir2', function() {
return {
restrict:'C',
template: 'subDir2'
}
});
抱歉,系统的每个指令增加3倍。任何人都可以解释确切的行为?
,WH我在子指令中使用templateUrl,它引发错误。同样的小提琴链接是[这里](https://plnkr.co/edit/bu6GfqKVafLcxPc7UFO7?p=preview) – Teja