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
.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);
}]);
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 :
Most likely you have somewhere ng-controller="ProfileCtrl", which causes an instantiation (a second one) of ProfileCtrl outside of the state's "resolve". Check templates/settings.html - it's probably there.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.