我正在使用模板 URL 创建指令。我想根据 user_role 动态设置模板 URL。任何想法?
这是我的指令代码:
RatingRX.directive "headermenu", ->
directive = {}
directive.restrict = 'E'
directive.templateUrl = "../assets/common/headerMenu{{user_role}}.html"
directive
我想从控制器设置 user_role 。例如:
$scope.user_role = 1
您可以将函数传递给 templateUrl 选项并返回一个字符串,该字符串将在最后用作模板 url。
首先,将角色作为属性分配到元素上(其中 userRole 绑定到作用域),如下所示:
<div my-directive user-role="{{userRole}}></div>
那么该指令可以读作:
myApp.directive('myDirective', function() {
return {
restrict: 'A',
templateUrl: function(element, attrs) {
return "../assets/common/headerMenu" + attrs.userRole + ".html";
}
}
});
Update:这以前适用于旧版本的 Angular。
<div ng-if="userRole === 'admin'" my-directive user-role="admin"></div>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)