0

I have the following scenario. I am trying to write a route system for a dynamic sidebar. My sidebar consists of several items (at least 10) and each item has at least 1 tab(could be 3 or 5 ...). the following should give an idea:

item1 -> tab1-tab2-tab3 ...

item2 -> tabA-tabB-tabC ...

.

.

.

itemX -> tabX-tabY-tabZ ...

I know that using state $stateprovider you can set the url and templateUrl to load different html contents(external html files) depending on what the user clicks. So to accomplish that I would have to write a bunch of states and I feel that would be bad programming and there must be a way to do a dynamic routing depending on what the user clicks. I have googling it and I cant quite find the right answer for this.

The trick is that I have to use ui.router for this. Any ideas or guides that I can read/use to accomplish this?

2
  • Have you looked at the documentation for decorator? angular-ui.github.io/ui-router/site/#/api/… "This can be used to add custom functionality to ui-router, for example inferring templateUrl based on the state name." Commented Oct 7, 2015 at 14:37
  • 0 down vote accept I actually took a different path. I had a json object where I was building the tabs from and figured that since I already have this object I would just add a path property to the json obj and have a for loop iterate the json and build my states in the config phase. It works and displays different .html files for each tab. I will definitively drill down more into decorate, it seems another better way of doing what I need. Thanks!!! Commented Oct 7, 2015 at 20:07

1 Answer 1

2

I would assume that since your sidebar is dynamic that it's coming from some data source. You could configure that datasource to have all the info you need for your routes or go with convention. Something like:

angular.forEach(states, function(state) {
    $stateProvider.state(state, {
            url: '/' + state, 
            templateUrl: 'views/' + state + '.html',
            controller: state + 'Controller'
      }) ;
})
Sign up to request clarification or add additional context in comments.

2 Comments

Yes, that is what I ended up doing. The only problem that Im debugging is that when I click on itemA and tabA previously having clicked on item1 and tab1, the content still remains there ... Any way to reset to blank the content when a new item is click from the sidebar?
That should happen automatically. I was gonna post a code snippet, but they really suck in comments. You should probably accept the answer above and then open a new question. I'm assuming you have something like <div ui-view>Nothing to see here yet</div> in your index.html.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.