简体   繁体   English

未知提供者:在AngularJS中使用带有resolve的工厂

[英]Unknown provider: using a factory with resolve in AngularJS

I keep getting error when I try to download data using a factory. 尝试使用工厂下载数据时,我总是收到错误消息。 The factory method is called under resolve in app.js 在app.js中的resolve下调用factory方法

app.js app.js

.state('app.settings', {
                url: '/settings',
                views: {
                    'menuContent': {
                        templateUrl: 'templates/settings.html',
                        resolve: {
                            profileData: function (ProfileFactory) {
                                return ProfileFactory.getProfile();
                            }
                        },
                        controller: 'ProfileCtrl'
                    }
                }
            })

Factory.Js Factory.Js

.factory('ProfileFactory', ['$q', '$http', 'localstorage', 'loginSharedData', 'Base64', function ($q, $http, localstorage, loginSharedData, Base64) {
        var urlBase = 'http://localhost/snc/Service.Svc/';
        var profileData = {};
        var deferred = $q.defer();
        var promise = deferred.promise;

        var _postData = {
            "apiKey": Base64.decode(localstorage.get('secureToken')),
            "userId": Base64.decode(loginSharedData.userid)
        };

        profileData.getProfile = function() {
            console.log('inside get profile service');
            $http({
                method: 'POST',
                url: urlBase + 'GetUserProfile',
                data: JSON.stringify(_postData),
                contentType: 'application/json',
                dataType: "json"
            }).then(function (response) {
                deferred.resolve(response.data.GetUserProfileResult);
            }, function () {
                deferred.reject('Failed to get profile');
            });

            promise.success = function (fn) {
                promise.then(fn);
                return promise;
            }
            promise.error = function (fn) {
                promise.then(null, fn);
                return promise;
            }
            return promise;
        }
        return profileData;
    }])

Controllers.js Controllers.js

.controller('ProfileCtrl', ['$scope', 'Base64', 'profileData', function ($scope, Base64, profileData) {
        $scope.profile={};

        if(profileData.status) {
            $scope.profile.email = Base64.decode(profileData.email);
            $scope.profile.fullname = Base64.decode(profileData.full_name);
            $scope.profile.username = Base64.decode(profileData.user_name);
            $scope.profile.emailstatus = profileData.email_status;
        }
        console.log($scope.profile);
    }]);

The Error: 错误:

Error: [$injector:unpr] Unknown provider: profileDataProvider <- profileData <- ProfileCtrl
http://errors.angularjs.org/1.3.13/$injector/unpr?p0=profileDataProvider%20%3C-%20profileData%20%3C-%20ProfileCtrl
minErr/<@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:8890:12
createInjector/providerCache.$injector<@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:12824:19
getService@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:12971:39
createInjector/instanceCache.$injector<@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:12829:28
getService@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:12971:39
invoke@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:13003:1
$ControllerProvider/this.$get</</<@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:17281:11
nodeLinkFn/<@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:16527:13
forEach@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:9158:11
nodeLinkFn@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:16526:11
compositeLinkFn@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:15905:13
publicLinkFn@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:15784:30
self.appendViewElement@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:48439:5
ionicViewSwitcher.create/switcher.render@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:46629:29
ionicViewSwitcher.create/switcher.init@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:46549:11
self.render@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:48295:5
self.register@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:48253:5
updateView@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:53645:11
.compile/</<@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:53622:11
$RootScopeProvider/this.$get</Scope.prototype.$broadcast@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:23547:15
transitionTo/$state.transition<@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:41017:11
processQueue@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:22016:27
scheduleProcessQueue/<@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:22032:27
$RootScopeProvider/this.$get</Scope.prototype.$eval@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:23228:16
$RootScopeProvider/this.$get</Scope.prototype.$digest@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:23044:15
$RootScopeProvider/this.$get</Scope.prototype.$apply@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:23333:13
done@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:18486:36
completeRequest@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:18676:7
requestLoaded@http://localhost:63342/SNC/www/lib/ionic/js/ionic.bundle.js:18617:1
consoleLog/<()ionic.bundle.js (line 20434)
$ExceptionHandlerProvider/this.$get</<(exception=
Error: [$injector:unpr] Unknown provider: profileDataProvider <- profileData <- ProfileCtrl
http://errors.angularjs.org/1.3.13/$injector/unpr?p0=profileDataProvider%20%3C-%20profileData%20%3C-%20ProfileCtrl


return new ErrorConstructor(message);

, cause=undefined)ionic.bundle.js (line 17384)
$RootScopeProvider/this.$get</Scope.prototype.$broadcast()ionic.bundle.js (line 23549)
transitionTo/$state.transition<()ionic.bundle.js (line 41017)
processQueue(state=Object { status=1, value={...}, processScheduled=false, more...})ionic.bundle.js (line 22016)
scheduleProcessQueue/<()ionic.bundle.js (line 22032)
$RootScopeProvider/this.$get</Scope.prototype.$eval(expr=function(), locals=undefined)ionic.bundle.js (line 23228)
$RootScopeProvider/this.$get</Scope.prototype.$digest()ionic.bundle.js (line 23044)
$RootScopeProvider/this.$get</Scope.prototype.$apply(expr=undefined)ionic.bundle.js (line 23333)
done(status=200, response="{"GetUserProfileResult":...user_name":"aGl0aW4="}}", headersString="Content-Type: application/json; charset=utf-8\r\n", statusText="OK")ionic.bundle.js (line 18486)
completeRequest(callback=done(status, response, headersString, statusText), status=200, response="{"GetUserProfileResult":...user_name":"aGl0aW4="}}", headersString="Content-Type: application/json; charset=utf-8\r\n", statusText="OK")ionic.bundle.js (line 18676)
requestLoaded()ionic.bundle.js (line 18617)


return logFn.apply(console, args);

The data is downloaded from the service and I can see the data in console log, however once the data download is finished, this error is thrown. 数据是从服务下载的,我可以在控制台日志中看到数据,但是一旦数据下载完成,就会引发此错误。 I can't figure out the issue. 我不知道这个问题。

As noted above by New dev : 如上所述, New dev指出:

Most likely you have somewhere ng-controller="ProfileCtrl", which causes an instantiation (a second one) of ProfileCtrl outside of the state's "resolve". 您很可能在某个地方有ng-controller =“ ProfileCtrl”,这会导致在状态的“解决”之外实例化ProfileCtrl(第二个)。 Check templates/settings.html - it's probably there. 检查template / settings.html-可能在那里。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM