mgftestdev01
mgftestdev01

Reputation: 23

How to use angularjs routeParams properly

I'm just wondering how to use routeParams correctly. I wrote code like this in app.js:

.when('/product', {
    templateUrl: 'views/product.html',
    controller: 'ProductController',
    title: ''
})
.when('/product/:productId', {
    templateUrl: 'views/product_detail.html',
    controller: 'ProductDetailController',
    title: ''
})
.when('/product/new', {
    templateUrl: 'views/product_new.html',
    controller: 'ProductController',
    title: ''
})

Here's the thing: I want to use URI named '/product/new' but it goes '/product/:productId'.. (product_detail.html)

I want to how to use '/product/:productId' and '/product/new' both.

Is there any way to route like that?

Upvotes: 2

Views: 39

Answers (1)

Ajay Narain Mathur
Ajay Narain Mathur

Reputation: 5466

Change the order in which you do routing to:

.when('/product', {
     templateUrl: 'views/product.html',
     controller: 'ProductController',
     title: ''
})
.when('/product/new', {
      templateUrl: 'views/product_new.html',
      controller: 'ProductController',
      title: ''
})
.when('/product/:productId', {
      templateUrl: 'views/product_detail.html',
      controller: 'ProductDetailController',
      title: ''
})

Upvotes: 2

Related Questions