获取指令模板中的$rootScope网址



是否可以在指令的 templateUrl 中获取$rootScope对象?
我的意思是,如果这是我的指令的功能:

function AttributesCard() {
return {
restrict: "AE",
templateUrl: function ($rootScope) {
return $rootScope.baseUrl +  "/attributesCard.directive.html";
},
controller: AttributesCardController,
controllerAs: "vm",
scope: {
educationPlace: "=",
privileges: "="
},
bindToController: true
};
}  

如何在模板 URL 的函数中获取$rootScope对象?我里面有别的东西。或者,也许不是$rootScope可能为我获取此数据的服务。

谢谢,
阿西隆

是的,您可以在指令中注入任何内容,就像在控制器、服务等中一样:

app.directive("name", ["$rootScope", "myService", function($rootScope, myService) {
// ..
templateUrl: function () {
var baseUrl = $rootScope.baseUrl; // or myService.baseUrl;
return baseUrl +  "/attributesCard.directive.html";
},
// ..
}]);

最新更新