Can I set a default value for a route parameter in AngularJS? Is there a way to have /products/123 and /products/ handled by the same route?
I am looking for refactoring existing code that looks like this:
myModule.config(['$routeProvider', function($routeProvider) { $routeProvider. when('/products/', {templateUrl: 'products.html', controller: ProductsCtrl}). when('/products/:productId', {templateUrl: 'products.html', controller: ProductsCtrl}) }]); function ProductsCtrl($scope, $routeParams) { $scope.productId = typeof($routeParams.productId) == "undefined" ? 123 : $routeParams.productId; }
It works, but it is not very elegant. Is there a better way?
javascript angularjs model-view-controller url-routing
mikel Sep 21 2018-12-12T00: 00Z
source share