我创建了以下角度指令,即在ParentDirective中使用的ChildDirective
var wizardModule = angular.module('Wizard', []);
wizardModule.directive('childDirective', function ($http, $templateCache, $compile, $parse) {
return {
restrict: 'E',
scope: [],
compile: function (iElement, iAttrs, transclude) {
iElement.append('child directive<br />');
}
}
})
wizardModule.directive('parentDirective', function ($http, $compile) {
return {
restrict: 'E',
compile: function (element, attrs) {
var x = '<child-directive></child-directive><child-directive></child-directive>';
element.append(x);
}
}
这工作正常,并且出现了几个子指令。
我想更新ParentDirective,以从服务器获取childDirectives列表。因此,我更新了ParentDirective代码以进行 ajax 调用,然后绘制ChildDirectives
var elem;
wizardModule.directive('parentDirective', function ($http, $compile) {
return {
restrict: 'E',
compile: function (element, attrs) {
var controllerurl = attrs.controllerurl;
elem = element;
if (controllerurl) {
$http.get(controllerurl + '/GetWizardItems').
success(function (data, status, headers, config) {
var x = '<child-directive></child-directive><child-directive></child-directive>';
elem.append(x);
$compile(x);
});
}
}
}
});
问题是childDirectives不再出现,尽管在调试器中它正在进入 childDirective 的编译方法