我能够根据您使用顶级自定义预编译指令的想法解决它,但是使用不同的实现方式:我正在运行BFS算法,其中每个摘要周期我尝试编译任何直接子预包含。每次迭代时,我都会等待包含子项计数为零,然后再检查是否需要另一个周期(使用scope.$watch)。完成后,我编译整个my-pre以解析任何{{xxxx}}并将其转换为pre。
Working plunkr
.directive('myHighlighter', function($compile, $timeout) {
return {
restrict: 'E',
controller: function() {},
link: function(scope, element, attrs, controller) {
$timeout(function() {
$(element).replaceWith($('<pre>' + element.text() + '</pre>'));
});
}
}
})
.directive('myPre', function($compile, $timeout) {
return {
restrict: 'E',
controller: function() {},
link: {
pre: function(scope, element, attrs, controller) {
scope.it = 100;
scope.incCount = 0;
scope.$watch('incCount', function(newVal, oldVal, scope) {
if (oldVal !== newVal && newVal === 0) {
var children = $('pre-include', element).length;
if (children !== 0) {
$compile(element)(scope);
} else {
var e2 = $('<my-highlighter>' + element.html() + '</my-highlighter>');
$(element).replaceWith(e2);
$compile(e2)(scope);
}
}
});
},
post: function(scope, element, attrs, controller) {
}
}
}
})
.directive('preInclude', function($templateRequest, $compile) {
return {
link: {
pre: function(scope, element, attrs, myPre) {
scope.incCount++;
},
post: function(scope, element, attrs, myPre, transclude) {
scope.it--;
if (scope.it <= 0) {
console.log("max iterations reached, stopping");
return;
}
$templateRequest(attrs.src).then(function(data) {
element.replaceWith(data);
scope.incCount--;
});
}
}
};
})