从指令访问$ rootScope变量

时间:2016-02-18 11:17:07

标签: javascript angularjs angularjs-directive scope angularjs-scope

所以我的指令看起来像这样:

(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")));

我有一个像这样定义的布局变量:

$rootScope.layout = "test";

在node指令中。不显示布局变量。

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

这显示为空。

如何从我的节点指令访问$ rootScope.layout?

2 个答案:

答案 0 :(得分:2)

尝试这种方式:

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

答案 1 :(得分:2)

您好,您需要在指令中注入$rootScope并尝试这种方式:

{{$root.layout | json}}
相关问题