Corner routing

I am working on a fairly simple AngularJS project with some nesting depth for routes to select from a nested data structure:

angular.module('doccat', []). config(['$routeProvider', function($routeProvider) { $routeProvider. when('/', { templateUrl: 'partials/detail.html', controller: DocDetailCtrl }). when('/:p0', { templateUrl: 'partials/detail.html', controller: DocDetailCtrl }). when('/:p0/:p1', { templateUrl: 'partials/detail.html', controller: DocDetailCtrl }). when('/:p0/:p1/:p2', { templateUrl: 'partials/detail.html', controller: DocDetailCtrl }). when('/:p0/:p1/:p2/:p3', { templateUrl: 'partials/detail.html', controller: DocDetailCtrl }). when('/:p0/:p1/:p2/:p3/:p4', { templateUrl: 'partials/detail.html', controller: DocDetailCtrl }). otherwise({ redirectTo: '/' }); }]); function DocDetailCtrl($scope, $routeParams) { var path = []; if ($routeParams.p0) path.push($routeParams.p0); if ($routeParams.p1) path.push($routeParams.p1); if ($routeParams.p2) path.push($routeParams.p2); if ($routeParams.p3) path.push($routeParams.p3); if ($routeParams.p4) path.push($routeParams.p4); // do stuff with path } 

There should be up to 5 layers on the way, which should be enough for my purposes, so at the moment this is good enough. However, the underlying data can be nested arbitrarily deeply, which will require arbitrary routing.
I think that the ideal route would be that it says "the rest of the path goes to any set of parameters", but it doesn't seem like there is a way to do something like this in AngularJS. Just for the sake of completeness, does anyone know how to do this?

+4
source share
1 answer

Check $ routeProvider in angularjs $ routeProvider document .

In particular, in the definition of β€œwhen,” you can read: β€œa path can contain named groups starting with a star (* name). All characters are impatiently stored in $ routeParams with the given name when the route matches.” Therefore, you just need to make sure that the route matches, and you will get a lot of parameters stored in $ routeParams.

Hope this helps.

+1
source

Source: https://habr.com/ru/post/1488108/


All Articles