AngularJS regex route for similar url to load different controller and view

前端 未结 3 1968
青春惊慌失措
青春惊慌失措 2020-12-06 02:52

I have a similar route that should load a different view and controller based on whether or not the parameter is a number. Example:

  • /artists/2 sho
相关标签:
3条回答
  • 2020-12-06 03:03

    Another way is to use the ui-router which supports regular expressions for routes (among a slew of other things) which would allow:

    $stateProvider.state("artists-index", {
      url: "/artists/{page:[0-9]*}",
      templateUrl: "/www/artists/index.html",
      controller: "ArtistsIndexController"
    });
    
    $stateProvider.state("artists-profile", {
      url: "/artists/{name}",
      templateUrl: "/www/artists/profile.html",
      controller: "ArtistsProfileController"
    });
    
    0 讨论(0)
  • 2020-12-06 03:21

    I'm using this as a solution for now and would be interested in alternatives!

    1) Create a generic template that will load in a controller and view dynamically:

    <div ng-controller="controller" ng-include src="templateUrl"></div>
    

    In this example I placed this view in /www/shared/dynamic-controller.html

    2) Create a controller that checks the route params to determine which controller and view to load:

    angular.module('appName').
      controller('ArtistsDynamicRouteController', ['$scope', '$controller', '$routeParams', function($scope, $controller, $routeParams) {
        if(/^\d+$/.test($routeParams.pageOrId)) {
          // when pageOrId is a page (number) we want to load the ArtistsIndexController
          $scope.controller = $controller('ArtistsIndexController', { $scope: $scope }).constructor;
          $scope.templateUrl = '/www/artists/index.html';
        } else {
          // when pageOrId is an id (non-number) we want to load the ArtistsProfileController
          $scope.controller = $controller('ArtistsProfileController', { $scope: $scope }).constructor;
          $scope.templateUrl = '/www/artists/profile.html';
        }
      }]);
    

    3) Use one route regardless of the parameter type:

    // handles both /artists/2 and /artists/username
    $routeProvider.when("/artists/:pageOrName", {
      templateUrl: "/www/shared/dynamic-controller.html",
      controller: "ArtistsDynamicRouteController"
    });
    
    0 讨论(0)
  • 2020-12-06 03:24

    I use a nasty hack, that consist on change the regexp

    var $route = $routeProvider.$get[$routeProvider.$get.length-1]({$on:function(){}});
    
    
    $routeProvider.when("/artists/:page", {
      templateUrl: "/www/artists/index.html",
      controller: "ArtistsIndexController"
    });
    
    $routeProvider.when("/artists/:name", {
      templateUrl: "/www/artists/profile.html",
      controller: "ArtistsProfileController"
    });
    
    $route.routes['/artist/:page'].regexp = /^\/(?:artist\/(\d+))$/
    $route.routes['/artist/:page/'].regexp = /^\/(?:artist\/(\d+))\/$/
    

    It's ugly but it works fine. You can change your routes' regexps to make them match whatever you want.

    0 讨论(0)
提交回复
热议问题