Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/22.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ssis/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs Can';t从其中一个控制器访问绑定到$rootScope的authData_Angularjs_Angularfire_Rootscope - Fatal编程技术网

Angularjs Can';t从其中一个控制器访问绑定到$rootScope的authData

Angularjs Can';t从其中一个控制器访问绑定到$rootScope的authData,angularjs,angularfire,rootscope,Angularjs,Angularfire,Rootscope,我有一个让我发疯的bug,基本上我所要做的就是设置一个$rootScope.authData属性,该属性保存用户的身份验证信息,以便我可以在不同的控制器中使用它 但是,当我尝试此操作时,它只会给我一个错误,即未定义$rootScope.authData。我已经检查过了,从mainCtrl将其记录到控制台时确实定义了它,但从tagsCtrl将其记录到控制台时,它是未定义的 考虑到我可以在另一个控制器中使用$rootScope.authData,这很奇怪。。此外,如果我在mainCtrl中添加$ro

我有一个让我发疯的bug,基本上我所要做的就是设置一个
$rootScope.authData
属性,该属性保存用户的身份验证信息,以便我可以在不同的控制器中使用它

但是,当我尝试此操作时,它只会给我一个错误,即未定义
$rootScope.authData
。我已经检查过了,从
mainCtrl
将其记录到控制台时确实定义了它,但从
tagsCtrl
将其记录到控制台时,它是
未定义的

考虑到我可以在另一个控制器中使用
$rootScope.authData
,这很奇怪。。此外,如果我在
mainCtrl
中添加
$rootScope.test='testing'
,并在
tagsCtrl
中记录控制台日志,它也可以工作

我看不出我在这里做错了什么,我已经走到了死胡同。有什么想法吗

设置
$rootScope.authData
的主控制器:

flickrApp.controller('mainCtrl', ['$scope', '$rootScope', '$firebase', 'Auth', function($scope, $rootScope, $firebase, Auth) {

    Auth.$onAuth(function(authData) {
        $rootScope.authData = authData;
        console.log($rootScope.authData); //Is defined here
    });
}]);
flickrApp.controller('tagsCtrl', ['$scope', '$rootScope', '$firebase', function($scope, $rootScope, $firebase) {

console.log($rootScope.authData); //Is not defined here

}]);
无法访问
$rootScope.authData
的控制器:

flickrApp.controller('mainCtrl', ['$scope', '$rootScope', '$firebase', 'Auth', function($scope, $rootScope, $firebase, Auth) {

    Auth.$onAuth(function(authData) {
        $rootScope.authData = authData;
        console.log($rootScope.authData); //Is defined here
    });
}]);
flickrApp.controller('tagsCtrl', ['$scope', '$rootScope', '$firebase', function($scope, $rootScope, $firebase) {

console.log($rootScope.authData); //Is not defined here

}]);
编辑:在收到Bricktop的一些反馈后,我尝试为此创建一个服务,结果如下:

flickrApp.service('shared', ['$scope', 'Auth', function($scope, Auth) {

    //Auth is a wrapper that points to my firebase reference

    Auth.$onAuth(function(authData) {
        return $scope.authData = authData;
    });
}]);
flickrApp.controller('tagsCtrl', ['$scope', '$firebase', 'shared', function($scope, $firebase, shared) {

    console.log($scope.authData.uid); //This would come from the 'shared' service now
}]);
我不确定这是否是有效的,但似乎不是,因为我收到了以下错误:

Error: [$injector:unpr] http://errors.angularjs.org/1.3.10/$injector/unpr?p0=%24scopeProvider%20%3C-%20%24scope%20%3C-%20shared
at Error (native)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:6:417
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:38:307
at Object.d [as get] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:36:308)
at https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:38:381
at d (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:36:308)
at e (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:64)
at Object.instantiate (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:213)
at Object.<anonymous> (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:490)
at Object.e [as invoke] (https://ajax.googleapis.com/ajax/libs/angularjs/1.3.10/angular.min.js:37:96)
这里出了什么问题?

以上解释了它(可能)不起作用的原因,但我建议您以不同的方式解决问题(在控制器之间共享变量)

您应该创建一个服务(类似于“共享”)并以这种方式共享数据。这是因为服务是单例的,而控制器不是。此外,即使您不需要变量,也不会向rootscope添加变量

查看此类共享服务检查的良好示例

以下是我的示例,它应该适用于您的示例:

首先,共享服务:

flickrApp.service('shared', function() {

    var authentication = 'none';

    return {
        getAuth: function () {
                return authentication;
        },
        setAuth: function (auth) {
            authentication = auth;
        }
    };
});
我喜欢保持这个共享服务与所有逻辑无关,并且只用于共享数据

下一步是主控制器,您必须确保在登录后调用任何其他控制器之前实际调用了它(因此您将把它放在处理登录的控制器中!)

最后,需要检查登录状态的任何其他控制器:

flickrApp.controller('tagsCtrl', ['$scope', '$firebase', 'shared', function($scope, $firebase, shared) {
    $scope.auth = shared.getAuth();
    if($scope.auth === 'none'){
      console.log('you are not logged in!');
    }else{
        console.log('welcome '+$scope.auth.uid);
        //anything you would do if a user is logged in
    }
}]);
我假设您知道以下内容,但我只想重复一下(我不熟悉firebase):

请始终注意,javascript代码可能会被恶意用户操纵,请确保在发送到服务器的每个http请求中都发送一个用户令牌,以确保用户实际登录,并且不要依赖javascript为您执行此操作


如果您有任何其他问题,请告诉我。

这里是一个通过共享服务和范围共享数据的简单示例

js

(function(app) {

    function SharedService() {
        this.user = {};
    }

    function Controller1(scope, SharedService) {
        scope.sharedService = SharedService;
    }

    function Controller2(scope, SharedService) {
        scope.sharedService = SharedService;
    }

    app.service('SharedService', SharedService);
    app.controller('Controller1', Controller1);
    app.controller('Controller2', Controller2);

})(angular.module('myApp', []));
html

<script src="https://code.angularjs.org/1.3.4/angular.js"></script>
<script type="text/javascript" src="js/exp2/app.js"></script>
<div ng-app="myApp">
    <div ng-controller="Controller1 as ctrl">
        </br> 
        <table>
            <tr>
                <td><B> Enter Age in Controller1</B></td>
                <td><input type="text" ng-model="ctrl.userAge"></select></td>
            </tr>
        </table>
    </div>
    <div ng-controller="Controller2 as ctrl">
        <table>
            <tr>
                <td><B> Age in controller2 : </B></td>
                <td>{{ctrl.userAge}}</td>
            </tr>
        </table>
    </div>
</div>


在Controller1中输入年龄 控制器中的年龄2: {{ctrl.userAge}
请参阅以下链接,了解如何在不使用任何范围的情况下共享数据。

我试过了,但在什么地方失败了。看看更新后的问题。似乎出于某种原因,我甚至无法将服务注入控制器。。。精神失常。我编辑了最初的回复,如果您仍然无法添加服务,请告诉我,您可能忘记将其添加到index.html中作为参考。现在它似乎可以工作了,感谢您花时间帮助我,非常感谢。:)好的,很高兴听到;角度有时很难:D