所以,我有这个代码使用角度transclude
。嵌套ng-transclude角
angular.module('playground', []).directive('tagA', function() {
return {
replace: true,
transclude: true,
controller: function() {
console.log('controller tagA')
},
compile: function($element, $attrs, $link) {
console.log('compile tagA', $element.html());
return {
pre: function($scope, $element, $attrs, controller, $transclude) {
console.log('pre tagA', $element.html());
},
post: function($scope, $element, $attrs, controller, $transclude) {
console.log('post tagA', $element.html());
}
}
},
template: '<u><tag-b><div ng-transclude></div></tag-b></u>'
}
}).directive('tagB', function() {
return {
require: '^tagA',
transclude: true,
controller: function() {
console.log('controller tagB')
},
compile: function($element, $attrs, $transclude) {
console.log('compile tagB', $element.html());
return {
pre: function($scope, $element, $attrs, controller, $transclude) {
console.log('pre tagB', $element.html());
},
post: function($scope, $element, $attrs, controller, $transclude) {
console.log('post tagB', $element.html());
}
}
},
template: '<h1 ng-transclude></h1>'
}
});
而且标记
<tag-a>
Test
</tag-a>
我所试图做的是从父(塔加)的transcluded内容传递给孩子(TAGB)。上面的代码有效,但我不想在我的tagA
的模板中有<div ng-transclude></div>
。显然,使用<u><tag-b ng-transclude></tag-b></u>
不起作用。 (<u><tag-b ng-transclude></tag-b></u>
)不能工作吗?
后者的方式不起作用,因为transclude替换内部HTML,所以它会取代元素,以及'NG-transclude'将不再是内部元件上。 – Claies