leon.io
leon.io

Reputation: 2824

Defer template loading when using ng-controller

I'm trying to defer loading of my main controller/template (AppController) until I load the users profile from a service.

For all navigation routes, I'm using $routeProvider with resolve

.when('/edit/:editId', {
    templateUrl: 'editTemplate.html',
    controller: 'EditController' // this is a child controller of AppController
    resolve: {
         currentUser: ['svc.user', function(userSvc) { return userService.getUser(); }]
    }
// and so on..

But for AppController I'm using

How can I do a resolve for AppController? Essentially, I'd like a userService.getUser() which returns a promise to finish executing before AppController is actually run, I'm not sure how to do this using ng-init though (or if it's possible, i thought only expressions not promises could be executed).

Thanks for any help/assistance.

Upvotes: 3

Views: 1457

Answers (1)

Brian Petro
Brian Petro

Reputation: 1567

As seen in this video: http://egghead.io/video/angularjs-resolve/

You should try:

.when('/edit/:editId', {
  templateUrl: 'editTemplate.html',
  controller: 'EditController' // this is a child controller of AppController
  resolve: {
     currentUser: ['svc.user', function(userSvc) { return userService.getUser(); }]
     app: function ($q) {
                var defer = $q.defer();
                defer.resolve();
                return defer.promise;
     }
}

Upvotes: 1

Related Questions