【问题标题】:Provider 'kk' must return a value from $get factory method提供者 'kk' 必须从 $get 工厂方法返回一个值
【发布时间】:2017-03-19 12:31:08
【问题描述】:

一年半前,我编写了一个 Angular 应用程序,它使用 authSrv 在我的后端使用 API 登录和注销用户,它运行良好。我只是尝试再次运行相同的代码,我得到了 Error: [$injector:undef] Provider 'authSrv' must return a value from $get factory method.

这是我的authSrv(工厂)代码:

app.factory('authSrv', function ($rootScope, $http, $auth, $state, config, $q) {

    var deferred = $q.defer();

    this.login = function (email, password) {
        // Show loading dialog
        $rootScope.showLoading();

        var credentials = {
            email: email,
            password: password
        };

        return $auth.login(credentials)
            .then(
                function (result) {
                    $http.get(config.server + 'restricted/user')
                        .then(function (response) {
                            // Stringify the returned data to prepare it
                            // to go into local storage
                            var user = JSON.stringify(response.data.user);

                            // Set the stringified user data into local storage
                            localStorage.setItem('user', user);

                            // The user's authenticated state gets flipped to
                            // true so we can now show parts of the UI that rely
                            // on the user being logged in
                            $rootScope.auth = true;

                            // Putting the user's data on $rootScope allows
                            // us to access it anywhere across the app
                            $rootScope.currentUser = response.data.user;

                            // Remove loading dialog
                            $rootScope.hideLoading();

                            // Everything worked out so we can now redirect to
                            // the users state to view the data
                            $state.go('app.artists');

                            deferred.resolve(response.data.user);
                            // promise is returned
                            return deferred.promise;
                        });
                },
                function (error) {
                    // the following line rejects the promise
                    deferred.reject(error);

                    // Remove loading dialog
                    $rootScope.hideLoading();

                    // promise is returned
                    return deferred.promise;
                });
    };

    this.logout = function () {
        // Show loading dialog
        $rootScope.showLoading();

        $auth.logout().then(function () {
            // Remove the authenticated user from local storage
            localStorage.removeItem('user');

            // Flip authenticated to false so that we no longer
            // show UI elements dependant on the user being logged in
            $rootScope.auth = false;

            // Remove the current user info from rootscope
            $rootScope.currentUser = null;

            $state.go('app.auth').then(function () {
                // Remove loading dialog
                $rootScope.hideLoading();
            });
        });
    };
});

为什么我现在得到错误而不是当时?有什么变化吗?

【问题讨论】:

    标签: angularjs angular-factory


    【解决方案1】:

    您应该使用app.service(),而不是app.factory()。工厂是一个应该创建并返回服务实例的函数。你的函数是一个构造函数,它初始化this。这就是service() 的用途。

    【讨论】:

    • 是的,做到了。我重新检查了我的旧代码,当时它是服务。我只是复制了除app.service 之外的所有内容,并假设它是一家工厂。我感到很惭愧。我们可以删除这个问题吗? :D
    猜你喜欢
    • 2016-08-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-06-13
    相关资源
    最近更新 更多