1

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);
});
1
  • I usually return $promises from my Services and I resolve them in Controllers Commented Feb 26, 2015 at 16:27

1 Answer 1

1

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();
  }**
Sign up to request clarification or add additional context in comments.

1 Comment

The real problem was another one. RootCtrl is not loaded. So the solution for me was to load the variable in the service and then load the variable from the service in each subcontroller.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.