shiv_saz
shiv_saz

Reputation: 65

Attaching same child states to multiple parent states

I am working on a Front-end web application on Angular JS and using UI-router for routing.

Now i have a child state that is same across multiple states.

$stateProvider.state('app.user1.viewProfile1', {
        url: '/:profileURL',
        views: {
            '[email protected]': {
                component: 'profile'
            }
        }
    });
 $stateProvider.state('app.user2.viewProfile1', {
        url: '/:profileURL',
        views: {
            '[email protected]': {
                component: 'profile'
            }
        }
    });
 $stateProvider.state('app.user3.viewProfile1', {
        url: '/:profileURL',
        views: {
            '[email protected]': {
                component: 'profile'
            }
        }
    });

Is there a way to not replicate the viewProfile child and subsequent state definition, any way to append same child to multiple parents?

Upvotes: 1

Views: 569

Answers (1)

Aman Srivastava
Aman Srivastava

Reputation: 93

You can drop states in a loop and & iterate over the states like -

angular.forEach(["parent.state1","parent.state2","parent.state3"], function(parent) {
  $stateProvider.state(parent+".childState", {
      url: '/child',
      ...
  });
});

Upvotes: 2

Related Questions