AngularJS - Dynamic Routing with UI-Router

So, I have my fully functional static router in my awesome one-time application with a corner connection, and now I get the task of making specific routes dynamic.

Say so far, I had the following parametric paths:

  • ^/user/:userName
  • ^/product/:productName
  • ^/category/:categoryName

Now, they all still have to answer the way ^/:slugthat will make a call to the API-interface to obtain the type of the resource corresponding to the slug, and finally forwards the request to a particular controllerwith its concrete resolve, templateUrl, abstractand data.

Any thoughts?


( EDIT : I assume that the same functionality will be possible with route mirrors (aka transparent forward), where a route other than the one requested is launched, although changing the route is not publicly available. I'm still not aware of whether this is possible in angular or ui-router ...)

+4
source share
1 answer

I understood that. This is not very good, but it works beautifully.

^/:username ( 'profile'), ^/:productName ( 'products') ^/:categoryName ( 'category'), /:slug ResolverController. $stateProvider.

ResolverController - :

function ($state, $stateParams, resource) {
  switch (resource) {
  case 'user':
    $state.go('profile', {'username': $stateParams.slug});
    break;
  case 'product':
    $state.go('products', {'productName': $stateParams.slug});
    break;
  case 'category':
    $state.go('category', {'categoryName': $stateParams.slug});
    break;
  default:
    // 404
    break;
  }
}

resource. .

+3

All Articles