Does AngularJS $ routeProvider have a shorthand way to have multiple routes with the same settings?

I have both routes leading to the same view and controller (i.e. I just pass the identifier for access to $ routeParams and executing the controller logic on it):

$routeProvider
        .when('/about',
        {
            controller: 'AboutController',
            controllerAs: 'vm',
            templateUrl: 'about.html'
        })
        .when('/about/:id',
        {
            controller: 'AboutController',
            controllerAs: 'vm',
            templateUrl: 'about.html'
        });

It seems very repetitive. Is there a shorthand, something like this?

$routeProvider
            .when(['/about', '/about/:id'],
            {
                controller: 'AboutController',
                controllerAs: 'vm',
                templateUrl: 'about.html'
            })
+4
source share
1 answer

From the $routeProvidersource code, it seems to me that this is not possible. this.whentakes two parameters: pathand route. For a multiple path, it this.whenmust either take an array of paths as a parameter, or extract multiple paths from one line. I do not see any of these two methods.

this.when = function(path, route) {
    //copy original route object to preserve params inherited from proto chain
    var routeCopy = angular.copy(route);
    if (angular.isUndefined(routeCopy.reloadOnSearch)) {
      routeCopy.reloadOnSearch = true;
    }
    if (angular.isUndefined(routeCopy.caseInsensitiveMatch)) {
      routeCopy.caseInsensitiveMatch = this.caseInsensitiveMatch;
    }
    routes[path] = angular.extend(
      routeCopy,
      path && pathRegExp(path, routeCopy)
    );

    // create redirection for trailing slashes
    if (path) {
      var redirectPath = (path[path.length - 1] === '/')
            ? path.substr(0, path.length - 1)
            : path + '/';

      routes[redirectPath] = angular.extend(
        {redirectTo: path},
        pathRegExp(redirectPath, routeCopy)
      );
    }

    return this;
  };
+3

All Articles