【发布时间】:2026-01-14 19:10:01
【问题描述】:
好的,所以我想在动态生成的模板 url 中使用范围变量。所以我尝试了这个:
html
<my-directive type="{{ type }}"></my-directive>
js
angular.module('myApp', [])
.directive('myDirective', function () {
return {
templateUrl: function (tElement, tAttrs) {
return 'templates/myDirective.' + tAttrs.type + '.html';
};
};
});
我原以为tAttrs.type 会返回$scope.type 的值,但我最终得到了{{ type }}。这导致了 templates/myDirective.{{ type }}.html 的 templateUrl。
那么,我可以做些什么来获取范围变量的值而不是原始文本?
【问题讨论】:
标签: javascript angularjs angularjs-directive