1

Maybe the title isn't really clear.

I'm working on two nested Angular apps. Both have their own router built with ui-router. If I call a state that is unknown by the main router, I'd like it to search for this state in the sub-app router to get the templateUrl and url related to this state.

I thought about creating a service with a parser. It could parse the file to find the data I want. This solution is probably not the best option I have, that's why I wanted to know if there is a specific function/method in ui-router to achieve it. From what I read on ui-router doc, it seems not :/

Feel free to ask for more details or to suggest another solution which can match with my goal :)

isherwood
  • 58,414
  • 16
  • 114
  • 157
Arhyaa
  • 369
  • 1
  • 3
  • 21

1 Answers1

0

You can achieve this by using dynamic parameters in your $stateProvider configuration defined on your sub-module. So you have some anchored routes on the main module, and if there is a match, ui-router will simply fetch the associated template. If there is no matched absolute route/url, then ui-router falls back on your parameterised route, like so:

// anchored
$stateProvider
  .state('mainAppState', {
    url: '/anchored',
    controller: 'myCtrl'
 })

// sub module
 .state('subAppState', {
    url: /:parameter
    resolve: { // use the resolve and $stateParams to get some parameter with which you can make a request to your API }
    templateProvider: { // inject a service and use it to call your backend }

You can use a the parameter, which gets passed to $stateParams, which gets passed to your resolve block functions, which can retrieve some data or process the data from $stateParams, that can then be used by templateProvider to provide a meaningful fallback URL. That's because templateProvider receives all resolves. It will always wait for all promises in the resolve block to resolve one way or another before executing.

That way you can catch any unmatched URLs from the main app in your sub app. Hope this helps.

See my issue here: ui-router: async data in templateUrl function

Oh and one more thing - if you are going to mangle your JS in your build process, make sure you define your templateProvider function with .$inject = [ ... ]. If not, you'll get an unknown provider error. ngAnnotate will NOT catch the dependencies in your templateProvider function. Alternatively you can just use /* @ngInject */, which worked for me.

Community
  • 1
  • 1
natnai
  • 554
  • 2
  • 10
  • I'm not sure to get it. Your solution should work, but I don't know how I could use it with two routers. Both apps have to work independently, and I don't think your suggestion could make it. It can't get any information in a second router. Or maybe I didn't fully understand what you said, which is possible too. – Arhyaa Jun 26 '15 at 07:29
  • I accept your response as the answer. Even if it doesn't fully match the question, it allows me to create a dynamic router. I also use this article to achieve my goal : http://tech-blog.maddyzone.com/javascript/dynamic-routing-angularjs. Thanks anyway ! – Arhyaa Jun 26 '15 at 14:16
  • Glad it helped. I don't think there is any other way to create a dynamic router with ui-router at the moment. – natnai Jun 28 '15 at 07:32