javascript 用于类似 url 的 AngularJS 正则表达式路由以加载不同的控制器和视图

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18131834/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 10:49:58  来源:igfitidea点击:

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

javascriptangularjsroutes

提问by Gloopy

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/2should ArtistsIndexControllerwith a view /www/artists/index.html
  • /artists/nameshould ArtistsProfileControllerwith a view /www/artists/profile.html
  • /artists/2应该ArtistsIndexController有看法/www/artists/index.html
  • /artists/name应该ArtistsProfileController有看法/www/artists/profile.html

Ideally I would use something like:

理想情况下,我会使用类似的东西:

$routeProvider.when("/artists/:page", {
  templateUrl: "/www/artists/index.html",
  controller: "ArtistsIndexController"
});

$routeProvider.when("/artists/:name", {
  templateUrl: "/www/artists/profile.html",
  controller: "ArtistsProfileController"
});

Where :pageis a number and :nameis not.

哪里:page是一个数字,:name不是。

Note I see a related github issue(found from this question) but am wondering if there is a resolution or preferred solution.

注意我看到了一个相关的github 问题(从这个问题中找到),但我想知道是否有解决方案或首选解决方案。

回答by Gloopy

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

另一种方法是使用ui-router支持路由的正则表达式(在许多其他事情中),这将允许:

$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"
});

回答by pykiss

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.

它很丑,但效果很好。您可以更改路线的正则表达式,使它们与您想要的任何内容相匹配。

回答by Gloopy

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:

1)创建一个通用模板,将在控制器中加载并动态查看:

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

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

在这个例子中,我把这个视图放在 /www/shared/dynamic-controller.html

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

2)创建一个控制器,检查路由参数以确定加载哪个控制器和视图:

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:

3) 无论参数类型如何,都使用一个路由:

// handles both /artists/2 and /artists/username
$routeProvider.when("/artists/:pageOrName", {
  templateUrl: "/www/shared/dynamic-controller.html",
  controller: "ArtistsDynamicRouteController"
});