【发布时间】:2014-03-17 03:01:50
【问题描述】:
我在 routeProvider 模板中有一个自定义标签,它需要 directive 模板。 version 属性将由作用域填充,然后调用正确的模板。
<hymn ver="before-{{ week }}-{{ day }}"></hymn>
根据星期几和星期几,有多种版本的赞美诗。我期待使用该指令来填充正确的.html 部分。 templateUrl 没有读取该变量。
emanuel.directive('hymn', function() {
var contentUrl;
return {
restrict: 'E',
link: function(scope, element, attrs) {
// concatenating the directory to the ver attr to select the correct excerpt for the day
contentUrl = 'content/excerpts/hymn-' + attrs.ver + '.html';
},
// passing in contentUrl variable
templateUrl: contentUrl
}
});
excerpts 目录中有多个标记为before-1-monday.html、before-2-tuesday.html、...的文件
【问题讨论】:
-
如果您使用的是 AngularJS 1.5+,请查看这个优雅的解决方案:stackoverflow.com/a/41743424/1274852
标签: angularjs angularjs-directive