【发布时间】:2013-09-20 20:11:09
【问题描述】:
我已经看到在 Angular 的指令中拥有动态 templateUrl 的能力,但在研究时我还没有看到动态模板。
.directive('col', function () {
var template = '<div class="one" ng-transclude></div>';
return {
restrict: 'E',
scope: {},
transclude: true,
replace: true,
template: template,
link: function (scope, ele, attrs) {
if (attrs.two !== undefined) {
template = '<div class="two" ng-transclude></div>';
} else if (attrs.three !== undefined) {
template = '<div class="three" ng-transclude></div>';
} else {
template = '<div class="one" ng-transclude></div>';
}
console.log(template);
}
};
})
HTML:
<col three>Three</col>
<col two>Two</col>
<col>Nothing</col>
控制台显示正确:
<div class="three" ng-transclude></div><div class="two" ng-transclude></div><div class="one" ng-transclude></div>
但是输出显示默认/初始<div class="one" ng-transclude></div>
【问题讨论】:
-
您可能希望像这个问题stackoverflow.com/questions/10629238/… 一样使用
compile功能 -
@nuclearghost +1,这很好。谢谢。我必须记住这一点以备将来使用。
标签: angularjs angularjs-directive