它';是否可以通过angularjs指令中的属性更改templateURL?
我有以下指示:它';是否可以通过angularjs指令中的属性更改templateURL?,angularjs,angularjs-directive,attributes,Angularjs,Angularjs Directive,Attributes,我有以下指示: (function() { 'use strict'; function header() { return { templateUrl: '/app/home/homeheader.html' }; } angular.module('app').directive('appheader', header); })(); 由于我需要根据使用它的页面更改标题,我想如果我可以在DOM中执行以下操作: index.html <
(function() {
'use strict';
function header() {
return {
templateUrl: '/app/home/homeheader.html'
};
}
angular.module('app').directive('appheader', header);
})();
由于我需要根据使用它的页面更改标题,我想如果我可以在DOM中执行以下操作:
index.html
<appheader page="index"></appheader>
signUp.html
<appheader page="signUp"></appheader>
然后在指令中,指定我想要的模板
我在网上读了一些,但没有找到任何解决办法。有很多这样的函数
templateUrl
中给出的示例如下所示:
angular.module('docsTemplateUrlDirective',[])
.controller('controller',['$scope',function($scope){
$scope.customer={
名字:“娜奥米”,
地址:“1600圆形剧场”
};
}])
.directive('myCustomer',function(){
返回{
templateUrl:函数(元素、属性){
返回'customer-'+attr.type+'.html';
}
};
});
您可以将函数传递到模板URL
,检查页面
属性,尽情发挥想象力
请看一下文档
下面是一个让您开始学习的示例:
(function() {
'use strict';
function header() {
return {
templateUrl: function(elem, attrs) {
// check the attribute value
if (attrs.page === "home") {
return '/app/home/homeheader.html';
} else {
return '/app/home/signupheader.html';
}
// OR use the attribute value as part of the template
return '/app/home/' + attrs.page + 'header.html';
}
};
}
angular.module('app').directive('appheader', header);
})();