2

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?

2 Answers 2

1

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

1 Comment

Partially at the end you are 'sharing' the service where you'll have the code just one time, the repeated code is just boilerplate, however any change on the service will be reflected on any reference.
1

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>

Comments

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.