【发布时间】:2016-05-11 22:48:58
【问题描述】:
Angular 1.5 支持multi transclude。
值得注意的是,能够将动态数量的项目嵌入到指令中并在以后声明这些嵌入的名称和位置(例如在链接/编译中)会很有用。
为了进一步说明,我希望能够执行以下操作:
//Example usage of directive
<multi-slot-transclude-example>
<transclude1>TEST1</div>
<transclude2>TEST2</div>
<transclude3>TEST3</div>
<transclude4>TEST4</div>
.... dynamic number of items ...
</multi-slot-transclude-example>
//Example of directive that dynamically transcludes multiple items
angular.module("multiSlotTranscludeExample", [])
.directive("directiveName", function(){
return {
restrict: 'E',
transclude: {
't1': '?transclude1',
't2': '?transclude2',
//I'd like this list to be able to be defined non-statically (e.g. in link)
},
template: '<div ng-repeat="n in transcludedElementList">'
+ '<div ng-transclude="t{{n}"></div>'
+ '</div>'
};
})
请注意,在实现多重嵌入的指令声明中,我必须先了解声明时将被嵌入的项目数量。
有没有办法在链接中(或使用变通方法)做这样的事情,这将保持与当前嵌入提供的相同功能?
【问题讨论】:
标签: javascript angularjs angularjs-directive angularjs-ng-transclude