检查工厂响应然后继续控制器

时间:2014-11-17 12:30:20

标签: javascript angularjs

我已经构建了一个基本身份验证工厂,我在控制器中注入以检查用户是否已登录并返回用户信息(如果是这样)。问题在于,每当我尝试它时,即使用户已登录,我也会得到false返回,因为控制器要求提供数据,并且在获得响应之前工厂完成其过程。然后我必须重新加载页面,以便控制器能够访问用户信息。

这是工厂:

angular.module('moheera').factory('registerUserConfigFactory', ['$rootScope', function ($rootScope) {
   var _this = {};
   //Get the user info from the window element that has been injected in the index page on the server side
   _this.user = window.userInfo || false;
   //Update the user rootScope to be true
   if(_this.user){
      $rootScope.logged = true;
   }
   return _this;
}]);

这是一个控制器

'use strict';

angular.module('userModule').controller('profileUserControlller', ['registerUserConfigFactory', '$scope', function (registerUserConfigFactory, $scope) {
    $scope.userInfo = registerUserConfigFactory;   
}]);

我希望我能做的事情不会要求"承诺"因为这在控制器级别会令人困惑。我怎样才能做到这一点?

我改变了主意....我认为使用承诺会更好。

我已经制作了这个代码,它有效,但我觉得它可以做得更好,特别是我必须把它叫两次!!

'use strict';

angular.module('moheera').factory('registerUserConfigFactory', ['$window', '$rootScope', '$q', function ($window, $rootScope, $q) {
    //Get the user info from the window element that has been injected in the index page on the server side
    var _this = {};

    _this.user = false;

    _this.readUserInfo = function () {
        return  $window.userInfo || false;
    }

    //Update the user rootScope to be true
    _this.registerUser = function () {
        var deferred = $q.defer();
        deferred.resolve(_this.readUserInfo());
        deferred.promise.then(function (result) {
            _this.user = result;
            if(_this.user){
                $rootScope.logged = true;
            }
        });
    }

    return _this;
}]);

这是控制器端代码:

registerUserConfigFactory.registerUser();
$scope.userInfo = registerUserConfigFactory;

这是最后一次更新:

'use strict';

angular.module('moheera').factory('registerUserConfigFactory', ['$window', '$rootScope', '$q', function ($window, $rootScope, $q) {
    //Get the user info from the window element that has been injected in the index page on the server side
    var _this = {};

    _this.user = false;

    //Read user info
    _this.readUserInfo = function () {
        return _this.user = $window.userInfo || false;
    }

    //Clear user info from the browser and change the rootScope status to false
    _this.clearUserInfo = function () {
        $rootScope.logged = false;
        _this.user = $window.userInfo = false;
    }

    //register user info
    _this.setUser = function (user) {
        _this.user = $window.userInfo = user || false;
        if(_this.user){
            $rootScope.logged = true;
        }
    }

    //Get the user info
    _this.getUser = function () {
        var deferred = $q.defer();
        deferred.resolve(_this.readUserInfo());
        deferred.promise.then(function (result) {
            if(result){
                _this.user = result;
                $rootScope.logged = true;
            }
        });
        return _this.user;
    }

    return _this;
}]);

0 个答案:

没有答案