是否可以$rootScope
在指令的 templateUrl 中获取对象?
我的意思是,如果这是我的指令的功能:
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";
},
// ..
}]);
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句