未知提供商:在AngularJS中使用具有解决方案的工厂

时间:2015-09-01 09:02:10

标签: javascript angularjs wcf ionic-framework

当我尝试使用工厂下载数据时,我一直收到错误。工厂方法在app.js

中的resolve下调用

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

从服务下载数据,我可以在控制台日志中看到数据,但是一旦数据下载完成,就会抛出此错误。我无法弄清楚这个问题。

1 个答案:

答案 0 :(得分:0)

如上所述New dev

  

很可能你有某个地方ng-controller =“ProfileCtrl”,这导致在状态的“解析”之外的ProfileCtrl实例化(第二个)。检查templates / settings.html - 它可能就在那里。