Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

$rootScope variable in directive template

I am trying to access a variable from my $rootScope in a directives template. However, I can't access it.

The simplified template:

<div class="item">
    {{ serverRoot }}
</div>

And the directive:

ItemModule.directive('item', [function ($rootScope) {
    return {
        restrict: 'E',
        scope: {
            item: '='
        },

        templateUrl: 'js/modules/items/directives/templates/item.html',

        link: function (scope, iElement, iAttrs) {
        }
    };
}])

How can I access the variable $rootScope.serverRoot?

like image 452
Maeh Avatar asked Apr 17 '26 11:04

Maeh


1 Answers

You have defined a new scope for your directive with

scope: {
    item: '='
},

So it won't be part of your link -> scope or controller -> scope. You should be able to access it via

link: function (scope, iElement, iAttrs) {
    scope.serverRoot = $rootScope.serverRoot;    
}

Further, your actual directive declaration needs to look like

ItemModule.directive('item', [ '$rootScope', function ($rootScope) {
like image 87
Brad Gunn Avatar answered Apr 20 '26 00:04

Brad Gunn