admin管理员组文章数量:1289528
I cannot make nested transclusion work.
There are two directives, both of which declare they will transclude their content. When I nest them, the inner doesn't have any content.
Here is this fiddle, that demonstrates my problem.
Here is the code:
function Ctrl($scope) {
$scope.text = 'Neque porro quisquam est qui dolorem ipsum quia dolor...';
}
angular.module('transclude', [])
.directive('outer', function(){
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {},
template: '<div style="border: 1px solid black;">' +
'<div>Outer</div>' +
'<inner ng-transclude></inner>' +
'</div>'
};
}).directive('inner', function(){
return {
restrict: 'E',
transclude: true,
replace: true,
template :'<div style="border: 1px solid red;">' +
'<div>Inner</div>' +
'<div ng-transclude></div>' +
'</div>'
};
});
I cannot make nested transclusion work.
There are two directives, both of which declare they will transclude their content. When I nest them, the inner doesn't have any content.
Here is this fiddle, that demonstrates my problem.
Here is the code:
function Ctrl($scope) {
$scope.text = 'Neque porro quisquam est qui dolorem ipsum quia dolor...';
}
angular.module('transclude', [])
.directive('outer', function(){
return {
restrict: 'E',
replace: true,
transclude: true,
scope: {},
template: '<div style="border: 1px solid black;">' +
'<div>Outer</div>' +
'<inner ng-transclude></inner>' +
'</div>'
};
}).directive('inner', function(){
return {
restrict: 'E',
transclude: true,
replace: true,
template :'<div style="border: 1px solid red;">' +
'<div>Inner</div>' +
'<div ng-transclude></div>' +
'</div>'
};
});
Share
Improve this question
asked Oct 17, 2013 at 15:17
manolovnikolaymanolovnikolay
1311 silver badge6 bronze badges
2 Answers
Reset to default 9You should ng-transculde inside the inner directive since transclude replaces the inner html
angular.module('transclude', []).directive('outer', function(){
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<div style="border: 1px solid black;">' +
'<div>Outer</div>' +
'<inner><div ng-transclude></div></inner>' +
'</div>'
};
});
No change to inner directive needed.
I have updated the fiddle here
Another way to do this, which can be useful in self contained ponents is displayed in this JSFiddle
.directive('outer', function(){
return {
restrict: 'E',
replace: true,
transclude: true,
template: '<div style="border: 1px solid black;">' +
'<div>Outer</div>' +
'<inner></inner>' +
'</div>'
};
})
.directive('inner', function(){
return {
restrict: 'E',
replace: true,
require: '^outer',
template :'<div style="border: 1px solid red;">' +
'<div>Inner</div>' +
'<div ng-transclude></div>' +
'</div>'
};
});
This will pass the transclude: true
down the dom tree to the inner directive.
The downside of this is that the cannot be used by itself and in the jsfiddle it throws an ngTransclude: Orphan Directive Error
Because of this I require that the inner
directive be a child of the outer
directive, that way it will always have the transclusion passed down to it.
This is really nice to break up large directives into smaller ones.
本文标签: javascriptHow to make nested transclusion in angular workStack Overflow
版权声明:本文标题:javascript - How to make nested transclusion in angular work? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741419073a2377696.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论