ganjan
ganjan

Reputation: 7596

Accessing $rootScope variable from a directive

So I have a directive that look like this:

(function (module) {

    var node = function (RecursionHelper) {

        return {
            restrict: 'E',
            controller: 'mainController',
            scope: {
                node: '=n'
            },
            templateUrl: '/app/NSviewer/templates/parts/node.html',
            compile: function (element) {
                // Use the compile function from the RecursionHelper,
                // And return the linking function(s) which it returns
                return RecursionHelper.compile(element);
            }
        };

    };

    module.directive("node", node);


}(angular.module("anbud")));

And I have a layout variable that is defined like this:

$rootScope.layout = "test";

In the node directive. The layout variable is not displayed.

<pre>{{layout | json}}</pre>

This shows up as empty.

How can I access $rootScope.layout from my node directive?

Upvotes: 1

Views: 477

Answers (2)

Kudos
Kudos

Reputation: 1154

Hi you need to inject $rootScope in your directive and try it this way:

{{$root.layout | json}}

Upvotes: 2

karaxuna
karaxuna

Reputation: 26940

Try this way:

<pre>{{$root.layout | json}}</pre>

Upvotes: 2

Related Questions