简体   繁体   English

AngularJS $ rootScope变量存在,但不可访问

[英]AngularJS $rootScope variable exists, but not accessible

I set a $rootScope variable in one of my modules and now want to access that same $rootScope variable in another module. 我在一个模块中设置了$ rootScope变量,现在想在另一个模块中访问相同的$ rootScope变量。 Thus far I can see that in both modules the variable has been set properly, but when I try accessing the variable in $rootScope, I only get undefined. 到目前为止,我可以看到两个模块中的变量都已正确设置,但是当我尝试在$ rootScope中访问变量时,只会得到未定义的变量。

How can I access this variable without doing a factory/service workaround? 如何在不执行工厂/服务解决方案的情况下访问此变量? The variable is really simple and $rootScope should suffice for what I need. 该变量非常简单,$ rootScope应该足以满足我的需求。 I've put some generic sample code below to illustrate the issue: 我在下面放置了一些通用示例代码来说明问题:

file1.js file1.js

var app = angular.module('MyApp1', []);

app.controller('Ctrl1', ['$scope', '$rootScope', function($scope, $rootScope) {
    $scope.myFunc = function() {
        $rootScope.test = 1;
    }
}

file2.js file2.js

var app = angular.module('MyApp2', []);

app.controller('Ctrl2', ['$scope', '$rootScope', function($scope, $rootScope) {
    $scope.need_to_access_this = $rootScope.test; // undefined
    console.log($rootScope); // returns JS object w/ test property set to 1
}

I was just stuck in the same problem when I figured out that you have define those properties for $rootScope before the controllers or services load. 当我发现您在控制器或服务加载之前为$ rootScope定义了那些属性时,我只是陷入了同样的问题。 So what I did was set inital values when the application runs. 所以我要做的就是在应用程序运行时设置初始值。 In your case it will be like: 在您的情况下,它将像:

app.run(function($rootScope){
    $rootScope.test="variable";
})

` `

In Ctrl1 the $rootScope.test value is set inside the $scope.myFunc. Ctrl1 ,$ rootScope.test值设置在$ scope.myFunc内。

The problem is that you aren't calling that function, so the test property in $rootScope is never set. 问题是您没有调用该函数,因此从未设置$ rootScope中的test属性。

You need to call $scope.myFunc(); 您需要调用$scope.myFunc(); in Ctrl1 or set $rootScope.test = 1; Ctrl1或设置$rootScope.test = 1; dirrectly in the Controller: 直接在控制器中:

app.controller('Ctrl1', ['$scope', '$rootScope', function($scope, $rootScope) {
    $scope.myFunc = function() {
        $rootScope.test = 1;
    };

    $scope.myFunc();
}

or 要么

app.controller('Ctrl1', ['$scope', '$rootScope', function($scope, $rootScope) {
    $rootScope.test = 1;
}

EDIT: 编辑:

The above suggestions still remain valid, thus you need to call myFunc(). 以上建议仍然有效,因此您需要调用myFunc()。

But the problem with your code is that Ctrl1 belongs to MyApp1 and Ctrl2 belongs to MyApp2 . 但随着你的代码的问题是, Ctrl1属于MyApp1Ctrl2属于MyApp2

Every application has a single root scope ( docs here ) 每个应用程序都有一个根范围此处为文档

You will need to create Ctrl2 as a controller of MyApp1 : 您将需要创建Ctrl2作为一个控制器MyApp1

angular.module('MyApp1')
    .controller('Ctrl2', ['$scope', '$rootScope', function($scope, $rootScope) {
        $scope.need_to_access_this = $rootScope.test; // undefined
        console.log($rootScope); // returns JS object w/ test property set to 1
    }]);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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