3

I have the following 2 states defined in my app.config:

state('product-details', {
    url: '/products/:productId',
    templateUrl: '/pages/product-details/product-details.tmpl.html',
    controller: 'ProductDetailsController'
})

and

state('product-register', {
    url: '/products/register',
    templateUrl: '/pages/product-register/product-register.tmpl.html',
    controller: 'ProductRegisterController'
})

The problem I am facing is since both their URL patterns are similar, when I try to navigate to product-register state, the 'register' is interpreted by angularui-router as a productId and redirects me to product-details state instead.

After going through some similar questions on SO, I thought I would filter these URLs using regexes as explained here. But when I tried this, although the regex was matching properly in isolation, the state refused to change.

Then again after doing a bit of research I decided to merge the two states like this:

state('product-details', {
     url: '/products/:param',
    templateUrl: ['$stateParams', function($stateParams) {
        if($stateParams.param === 'register') {
            return '/pages/product-register/product-register.tmpl.html';
        } else {
            return '/pages/product-details/product-details.tmpl.html';
        }
    }],
    controller: ['$stateParams', function($stateParams) {
        if($stateParams.param === 'register') {
            return 'ProductRegisterController';
        } else {
            return 'ProductDetailsController';
        }
    }]
})

This also does not seem to work as expected. What am I missing? Isn't this a very normal thing you would have to do?

2
  • Can't you just describe your route as url: '/products/detail/:productId'? Commented Nov 9, 2015 at 9:21
  • I could, but that's not my point :) Commented Nov 9, 2015 at 9:22

2 Answers 2

3

Try to move 'product-register' state before 'product-details' state.

Actually you need to differentiate your state URL pattern or else angular will fire the first match it will find.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks! Funny that switching the order didn't pass the problem to the other state. Now both just work
1

You should be able to specify types for your parameters to match only certain states.

In your case you could use:

url: '/products/{productId:int}'

There are lots of other examples on the ui router page: https://github.com/angular-ui/ui-router/wiki/URL-Routing

4 Comments

This seems feasible, however my ids are Strings
Is there any defined pattern for your ids that you could use a regex for? Otherwise it might be difficult without differentiating urls more :/
You need to have uniq identifiers for states, one way, if you have some pattern could be using regular expressions (Ui.Router has a basic support on it)... Howewer, the best solution is having unique identifiers...
What if we can´t control that? Im my case I have no way of knowing if "/products" is a category or a cms page :/

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.