0

I've a page with Navbar and Sidebar that remains common across most of the pages and hence I load all data related to LoggedInUser in NavbarController.

This data ($scope.loggedInUser) is used in other Controllers (child controller) as well. Since call to get User data is async, many times, child controller tries to access data before it is returned from the server.

What is the best way to ensure, all promises of parent controller are resolved before child controller starts it's work?

Ashit Vora
  • 2,902
  • 2
  • 27
  • 39

2 Answers2

0

You should use resolve from $stateProvider. This function ensures that all the promises are resolved before loading a new state.

Deividas
  • 6,437
  • 2
  • 26
  • 27
0

A resolve is a property you can attach to a route in both ngRoute and the more robust UI router. A resolve contains one or more promises that must resolve successfully before the route will change. This means you can wait for data to become available before showing a view, and simplify the initialization of the model inside a controller because the initial data is given to the controller instead of the controller needing to go out and fetch the data.

$routeProvider
.when("/news", {
    templateUrl: "newsView.html",
    controller: "newsController",
    resolve: {
        message: function(messageService){
            return messageService.getMessage();
    }
}

})

in controller

app.controller("newsController", function (message) {
$scope.message = message;

});

Hope this will help you

Mayur Shah
  • 3,344
  • 1
  • 22
  • 41