tommyd456
tommyd456

Reputation: 10683

Sharing a "resolve" in ui-router

I have two states that need to use the same resolve:

.state('state', {
  url: '/somewhere',
  templateUrl: '/views/somewhere.html',
  controller: 'myController',
  resolve: {
    //resolve something here
  }
})

.state('state2', {
  url: '/somewhere-else',
  templateUrl: '/views/somewhere-else.html',
  controller: 'myController',
  resolve: {
    //resolve the same thing here
  }
})

But it would be nice not to write the same code so is it possible for states to share a resolve?

Upvotes: 2

Views: 71

Answers (2)

Whisher
Whisher

Reputation: 32726

You can use https://github.com/angular-ui/ui-router/wiki/Nested-States-%26-Nested-Views#dot-notation like:

.state('myparent', {
        abstract: true,
        templateUrl: 'views/myparent.html',
        resolve: {
            issessionedin: function(Sessions){
                return Sessions.isSessionedIn();
            } 
        }
    })
    .state('myparent.state', {
        url: '/somewhere',
        templateUrl: '/views/somewhere.html',
        controller: 'SomewhereController'
    })
    .state('myparent.state2', {
        url: '/somewhere-else',
        templateUrl: '/views/somewhere-else.html',
        controller: 'SomewhereElseController',

    })

in the myparent.html you should put

<div data-ui-view></div>

Upvotes: 1

pedrommuller
pedrommuller

Reputation: 16056

probably sharing a service method could do the trick for you.

.state('state', {
  url: '/somewhere',
  templateUrl: '/views/somewhere.html',
  controller: 'myController',
  resolve: {
    service:function(myService){
     return myService.someFunc();
    }
  }
})

.state('state2', {
  url: '/somewhere-else',
  templateUrl: '/views/somewhere-else.html',
  controller: 'myController',
  resolve: {
    service:function(myService){
     return myService.someFunc();
    }
  }
})

Upvotes: 1

Related Questions