ashilon ashilon - 2 years ago 122
AngularJS Question

Get $rootScope in a directive's templateUrl

Is it possible to get the

object in a directive's templateUrl?

I mean, if this is my directive's function:

function AttributesCard() {

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

how do I get the $rootScope object inside the templateUrl's function? I get someting else inside of it. Or perhaps instead of the $rootScope a service that might get this data for me.



Answer Source

Yes, you can inject anything in your directive just as in your controllers, services etc:

app.directive("name", ["$rootScope", "myService", function($rootScope, myService) {
    // ..
    templateUrl: function () {
        var baseUrl = $rootScope.baseUrl; // or myService.baseUrl;
        return baseUrl +  "/attributesCard.directive.html";
    // ..
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download