在指令的 templateUrl 中获取 $rootScope
Get $rootScope in a directive's templateUrl
是否可以在指令的 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
};
}
如何在 templateUrl 的函数中获取 $rootScope 对象?我在里面得到了其他东西。或者也许可以代替 $rootScope 为我获取此数据的服务。
谢谢,
阿希隆
是的,您可以在指令中注入任何内容,就像在控制器、服务等中一样:
app.directive("name", ["$rootScope", "myService", function($rootScope, myService) {
// ..
templateUrl: function () {
var baseUrl = $rootScope.baseUrl; // or myService.baseUrl;
return baseUrl + "/attributesCard.directive.html";
},
// ..
}]);
是否可以在指令的 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
};
}
如何在 templateUrl 的函数中获取 $rootScope 对象?我在里面得到了其他东西。或者也许可以代替 $rootScope 为我获取此数据的服务。
谢谢,
阿希隆
是的,您可以在指令中注入任何内容,就像在控制器、服务等中一样:
app.directive("name", ["$rootScope", "myService", function($rootScope, myService) {
// ..
templateUrl: function () {
var baseUrl = $rootScope.baseUrl; // or myService.baseUrl;
return baseUrl + "/attributesCard.directive.html";
},
// ..
}]);