簡體   English   中英

未知提供者:在AngularJS中使用帶有resolve的工廠

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

嘗試使用工廠下載數據時,我總是收到錯誤消息。 在app.js中的resolve下調用factory方法

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('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

.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);
    }]);

錯誤:

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);

數據是從服務下載的,我可以在控制台日志中看到數據,但是一旦數據下載完成,就會引發此錯誤。 我不知道這個問題。

如上所述, New dev指出:

您很可能在某個地方有ng-controller =“ ProfileCtrl”,這會導致在狀態的“解決”之外實例化ProfileCtrl(第二個)。 檢查template / settings.html-可能在那里。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM