简体   繁体   中英

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?

尝试这种方式:

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

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

{{$root.layout | json}}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM