Can angularjs routes have default parameter values?

后端 未结 5 1781
余生分开走
余生分开走 2020-12-05 17:21

Can I set a default value of a parameter of a route in AngularJS? Is there a way to have /products/123 and /products/ handled by the same route ?

5条回答
  •  南方客
    南方客 (楼主)
    2020-12-05 18:09

    Not sure if this question is specific to $routeProvider but in $stateProvider, you can achieve this by

    myApp.config(function($stateProvider) {
    
        $stateProvider
            .state('products', {
                url: '/:productId',
                templateUrl: "/dashboard/products.html",
                controller: 'ProductController',
                params: {
                    productId: {
                      value: "defaultValue",
                      squash: true // or enable this instead to squash `productId` when empty
                    } 
                }
            });
    });
    

提交回复
热议问题