1

基本的に私がしようとしている$rootScope.authDataのは、ユーザーの認証情報を保持するプロパティを設定して、別のコントローラーで使用できるようにすることだけです。

ただし、これを試してみると、$rootScope.authData定義されていないというエラーが表示されます。確認したところ、 からコンソールにログを記録するときに実際に定義されていますが、 からコンソールmainCtrlundefinedログを記録するときtagsCtrlです。

これは、他のコントローラーの 1 つで使用できるという事実を考えると奇妙です。また、コンソール ログ$rootScope.authDataを追加する$rootScope.test = 'testing'と、動作することがわかります。mainCtrltagsCtrl

私がここでやったことは何も悪いことではなく、私は行き止まりに達しました。何か案は?

を設定するメインコントローラ$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
    });
}]);

にアクセスできないコントローラー$rootScope.authData:

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;
    });
}]);

これが有効かどうかはわかりませんが、次のエラーが発生してからではないようです。

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)

私はそれを次のように注入します:

flickrApp.controller('tagsCtrl', ['$scope', '$firebase', 'shared', function($scope, $firebase, shared) {

    console.log($scope.authData.uid); //This would come from the 'shared' service now
}]);

ここで何が問題なのですか?

4

2 に答える 2

1

上記の回答は、(おそらく) 機能しない理由を説明していますが、別の方法で問題 (コントローラー間で変数を共有する) を解決することをお勧めします。

代わりに、サービス (「共有」のようなもの) を作成し、その方法でデータを共有する必要があります。これが機能するのは、サービスがシングルトンであるのに対し、コントローラーはシングルトンではないためです。さらに、変数が不要な場合でも持ち越されるルートスコープに変数を追加しません。

このような共有サービスの良い例を見るには、ここをチェックしてください。

あなたの例でうまくいくはずの私の例は次のとおりです。

まず、共有サービス:

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

    var authentication = 'none';

    return {
        getAuth: function () {
                return authentication;
        },
        setAuth: function (auth) {
            authentication = auth;
        }
    };
});

私は、この共有サービスをすべてのロジックから切り離し、データの共有にのみ使用することを好みます。

次は、ログイン後に他のコントローラーが呼び出される前に、これが実際に呼び出されることを確認する必要があるメインコントローラーです (したがって、ログインを処理するコントローラーに配置します!)

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

    Auth.$onAuth(function(authData) {
        shared.setAuth(authData);
        console.log(shared.getAuth()); //Check if it was set correctly
    });
}]);

最後に、ログイン状態を確認する必要があるその他のコントローラー:

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 に依存しないでください。あなたのために。

他に問題がある場合は、お知らせください。

于 2015-02-06T19:35:03.193 に答える