Skip to content

How do you resolve a Controller before loading it's children? #104

Closed
@mrrooijen

Description

@mrrooijen

I have currently run in to a situation where I have an abstract state which has a controller that does a bunch of things. One among which performs an async call using ngResource to fetch some data from the server and assigns it to a property on $scope. However, since the state manager (from what I can see) doesn't wait until this asynchronous class is finished it goes ahead and loads the underlying states and controllers. Now the child controllers are loaded and they cannot find the $scope.thatResourceProperty because the async call isn't finished yet.

I tried to do a simple $q.defer(); $q.resolve(); $q.promise in the controller to see if the state manager works with promises and waits until it flags it as "resolved". It doesn't look like it's working, although I might be doing something wrong.

I also tried the resolve property on the .state function in $stateProvider but it looks like I can't inject all the stuff I need that I'm injecting in to my controller. Is there a way to tell the state manager to wait for the controller to resolve? Like

.state "accounts",
  abstract: true
  resolve: true
  controller: "Accounts"
  templateUrl: "/app/templates/accounts"

At the moment I'm having a hard time figuring out how to go about waiting for the controller and all it's asynchronous actions to finish before loading child (nested) states/controllers.

Any pointers much appreciated!

Cheers

EDIT: Ideally I would just want to use my controller and not a separate resolve property on the state manager as to me it feels like it's out of place. The logic I want should be in the controller and thus I want the controller to finish everything including it's async actions before proceeding down the hierarchy.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions