Angularjs 控制器中未定义工厂变量

Angularjs 控制器中未定义工厂变量,angularjs,Angularjs,我想在一个问题上得到帮助。我已经为登录过程创建了AuthService。登录后,用户将被重定向到主页。主页有一个名为ApplicationController的控制器。此控制器正在监视AuthService的变量更改。在此步骤中,AuthService.getUser()返回未定义。我已经搜索了解决方案并应用了所有推荐的解决方案,但仍然没有任何更改。有人能帮我理解这个问题吗 LoginController.js app.controller('LoginController', ['$scope

我想在一个问题上得到帮助。我已经为登录过程创建了AuthService。登录后,用户将被重定向到主页。主页有一个名为ApplicationController的控制器。此控制器正在监视AuthService的变量更改。在此步骤中,
AuthService.getUser()
返回未定义。我已经搜索了解决方案并应用了所有推荐的解决方案,但仍然没有任何更改。有人能帮我理解这个问题吗

LoginController.js

app.controller('LoginController', ['$scope', '$window', 'AuthService', function ($scope, $window, AuthService) {
$scope.username = null;
$scope.password = null;
$scope.error = null;

$scope.login = function () {
    $scope.error = null;
    AuthService.login({ username: $scope.username, password: $scope.password },
        function success(data, status, headers, config) {

            // redirect to homepage, which has ApplicationController
            $window.location = "/";
        },
        function error(data, status, headers, config) {
            $scope.error = data.error_description;
        });
}
}]);
app.factory('AuthService', function ($http) {
var authService = {};

var currentUser;

authService.login = function (credentials, successCallback, errorCallback) {
    $http({
        // do some $http config
    }).success(function (data, status) {
        var loggedInUser = {
            Username: data.Username,
            Name: data.Name,
            Surname: data.Surname,
            Email: data.Email,
            ID: data.ID,
            MobileNumber: data.MobileNumber,
            Role: data.Role
        };
        currentUser = loggedInUser; // here set the currentUser
        successCallback(data, status);
    }).error(function (data, status) {
        errorCallback(data, status);
    });
};

authService.isLoggedIn = function () {
    return currentUser != undefined ? true : false;
};
authService.getUser = function () {
    return currentUser;
};

return authService;
})
app.controller('ApplicationController', function ($scope, $location, $window, AuthService) {
   $scope.currentUser;
   $scope.isAuthenticated;

   $scope.$watch(AuthService.isLoggedIn, function (isLoggedIn) {

   // here AuthService.getUser() returns undefined.   
   $scope.currentUser = AuthService.getUser(); 
   });
});
AuthService.js

app.controller('LoginController', ['$scope', '$window', 'AuthService', function ($scope, $window, AuthService) {
$scope.username = null;
$scope.password = null;
$scope.error = null;

$scope.login = function () {
    $scope.error = null;
    AuthService.login({ username: $scope.username, password: $scope.password },
        function success(data, status, headers, config) {

            // redirect to homepage, which has ApplicationController
            $window.location = "/";
        },
        function error(data, status, headers, config) {
            $scope.error = data.error_description;
        });
}
}]);
app.factory('AuthService', function ($http) {
var authService = {};

var currentUser;

authService.login = function (credentials, successCallback, errorCallback) {
    $http({
        // do some $http config
    }).success(function (data, status) {
        var loggedInUser = {
            Username: data.Username,
            Name: data.Name,
            Surname: data.Surname,
            Email: data.Email,
            ID: data.ID,
            MobileNumber: data.MobileNumber,
            Role: data.Role
        };
        currentUser = loggedInUser; // here set the currentUser
        successCallback(data, status);
    }).error(function (data, status) {
        errorCallback(data, status);
    });
};

authService.isLoggedIn = function () {
    return currentUser != undefined ? true : false;
};
authService.getUser = function () {
    return currentUser;
};

return authService;
})
app.controller('ApplicationController', function ($scope, $location, $window, AuthService) {
   $scope.currentUser;
   $scope.isAuthenticated;

   $scope.$watch(AuthService.isLoggedIn, function (isLoggedIn) {

   // here AuthService.getUser() returns undefined.   
   $scope.currentUser = AuthService.getUser(); 
   });
});
ApplicationController.js

app.controller('LoginController', ['$scope', '$window', 'AuthService', function ($scope, $window, AuthService) {
$scope.username = null;
$scope.password = null;
$scope.error = null;

$scope.login = function () {
    $scope.error = null;
    AuthService.login({ username: $scope.username, password: $scope.password },
        function success(data, status, headers, config) {

            // redirect to homepage, which has ApplicationController
            $window.location = "/";
        },
        function error(data, status, headers, config) {
            $scope.error = data.error_description;
        });
}
}]);
app.factory('AuthService', function ($http) {
var authService = {};

var currentUser;

authService.login = function (credentials, successCallback, errorCallback) {
    $http({
        // do some $http config
    }).success(function (data, status) {
        var loggedInUser = {
            Username: data.Username,
            Name: data.Name,
            Surname: data.Surname,
            Email: data.Email,
            ID: data.ID,
            MobileNumber: data.MobileNumber,
            Role: data.Role
        };
        currentUser = loggedInUser; // here set the currentUser
        successCallback(data, status);
    }).error(function (data, status) {
        errorCallback(data, status);
    });
};

authService.isLoggedIn = function () {
    return currentUser != undefined ? true : false;
};
authService.getUser = function () {
    return currentUser;
};

return authService;
})
app.controller('ApplicationController', function ($scope, $location, $window, AuthService) {
   $scope.currentUser;
   $scope.isAuthenticated;

   $scope.$watch(AuthService.isLoggedIn, function (isLoggedIn) {

   // here AuthService.getUser() returns undefined.   
   $scope.currentUser = AuthService.getUser(); 
   });
});

ApplicationController应该监视AuthService.isLoggedIn的返回值,因此:

$scope.$watch(function () {
    return AuthService.isLoggedIn();
}, function (isLoggedIn) {
不是:


$scope.$watch接受函数作为watch表达式,所以您可以返回isLoggedIn的值

app.controller('ApplicationController', function ($scope, $location, $window, AuthService) {
    $scope.currentUser;
    $scope.isAuthenticated;

    $scope.$watch(function () {return AuthService.isLoggedIn();}, function (isLoggedIn) {

    // here AuthService.getUser() returns undefined.   
    $scope.currentUser = AuthService.getUser(); 
    });
});
$scope.$watch(AuthService.isLoggedIn,function(isLoggedIn){仍在计算isLoggedIn的结果。因此这里没有任何错误。即使我更改了该部分,getUser也返回未定义的结果。