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

前端 未结 3 1987
青春惊慌失措
青春惊慌失措 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条回答
  •  旧时难觅i
    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.

提交回复
热议问题