Reputation: 4172
apothekenService.getApotheke returns a promise. response.data within the promise is a json object which is delivered correctly, I can see that in the first console.log.
How can I get this json object to my variable in the resolve? When I do the same in a controller I just use $scope and bind the variable within the response but in this case I don't have a scope.
angular.module("test", ["ngAnimate", "ngCookies", "ngTouch", "ngSanitize", "ngResource", "ui.router", "ui.bootstrap", "ui.bootstrap.showErrors", "myApothekenService"]).config(function($stateProvider, $urlRouterProvider, $uiViewScrollProvider, showErrorsConfigProvider) {
$uiViewScrollProvider.useAnchorScroll;
return $stateProvider.state("root", {
abstract: true,
controller: "RootCtrl",
resolve: {
Apotheke: function(apothekenService) {
this.apo = {};
apothekenService.getApotheke().then(function(response) {
console.log(response.data);
return this.apo = response.data;
});
console.log(this.apo);
return this.apo;
}
}
});
}).controller("RootCtrl", function($scope, $location, $anchorScroll, Apotheke) {
$scope.apotheke = Apotheke;
console.log($scope.apotheke);
});
Upvotes: 1
Views: 394
Reputation: 2977
You know this inside the promise resolve function (then) is different than the this inside the parent resolve function. If you want to use this pattern, the usual convention is to capture this in a local variable self, and use that throughout your resolve function.
But anyways, I will just do as below. This way, the controller code won't execute until the resolve promise is resolved.
Apotheke: function(apothekenService) {
return apothekenService.getApotheke();
}**
Upvotes: 1