对于类似的URL AngularJS正则表达式的路线加载不同的控制器和视图 [英] AngularJS regex route for similar url to load different controller and view

查看:147
本文介绍了对于类似的URL AngularJS正则表达式的路线加载不同的控制器和视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有应该加载基于该参数是否是数字的不同视图和控制器一个类似的路线。例如:

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


  • /艺术家/ 2 ArtistsIndexController 以期 / WWW /艺术家/指数html的

  • /艺术家/名称 ArtistsProfileController 以期 / WWW /艺术家/个人资料html的

  • /artists/2 should ArtistsIndexController with a view /www/artists/index.html
  • /artists/name should ArtistsProfileController with a view /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"
});

其中,:页是一个数字,:名称不是

请注意我看到了相关 GitHub的问题(从的this问题),但我想知道如果有一个分辨率或preferred解决方案。

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

推荐答案

另一种方法是使用

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

这篇关于对于类似的URL AngularJS正则表达式的路线加载不同的控制器和视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆