AngularJS - 找到 cookieStore 时调用登录控制器

AngularJS - Call Login Controller when cookieStore found

我有以下代码:

angular.module('myApp', ['ngRoute', 'ngCookies', ... , 'myApp.controllers'])
    .run(function($cookieStore) {
        console.log("Checking user credentials");
        if ($cookieStore.get('credentials') != null) {
            console.log("User credentials found in cookies");
            //LoginCtrl.login($cookieStore.get('credentials'));
        }
    })

此代码的目标是当应用程序运行时检查 cookiestore 以获取已保存的凭据。如果它找到凭据,我希望它从下面定义的登录控制器调用函数 login():

angular.module('myApp.controllers')
    .controller('LoginCtrl', function($scope, $log, $cookieStore, $location, $http, AuthenticationService, User) {

        $scope.login = function(credentials) {
            $log.debug("/POST to /api/login");
            AuthenticationService.login(credentials).success(function(user) {
                ...
            }).error(function(err) {
                ...
            });
        };

目前正在抱怨 LoginCtrl 不存在,我尝试了各种方法来尝试定义 LoginCtrl(myApp.controllers.LoginCtrl、myApp.LoginCtrl 等),但 none 似乎上班。

请问有什么建议吗?我不想在 cookie 中保存用户对象,我宁愿使用散列密码保存凭据。

感谢您提供的任何帮助和建议!

控制器之间共享代码的模式是服务,您需要创建一个通用服务来在控制器之间验证和使用此服务