在ui-router
的文档中,有一个名为UrlMatcher的对象我需要访问它才能使用某些方法(例如 exec
),但我找不到任何关于如何执行此操作的明确说明。然而,该对象在文档页面上进行了描述。
我希望能够做这样的事情:
new UrlMatcher('/user/{id}?q&r').exec('/user/bob', {
x: '1', q: 'hello'
});
我如何访问该对象?
最佳答案
Angular 有 $urlMatcherFactory and UrlMatchers 。
骗子: http://plnkr.co/edit/MdazdvpVKjZhNjI6ErAH?p=preview
angular.module('myApp',[]).run(['$urlMatcherFactory',
function($urlMatcherFactory) {
var sourceList= ['John', 'Paul', 'George', 'Ringo']
var names = sourceList.join('|');
var urlMatcher = $urlMatcherFactory.compile("/{source:(?:" + names + ")}/:id");
$stateProviderRef
.state('names', {
url: urlMatcher,
templateUrl: 'tpl.root.html',
controller: 'MyCtrl'
});
}
]);
引用: Discussion on ui-router's UrlMatcher 。
使用执行:
网址:/home/1?param1=tt
var urlMatcher = $urlMatcherFactory.compile("/home/:id?param1");
var matched = urlMatcher.exec($location.path(), $location.search());
您将获得 2 个字段
:id ==> 1 和 param1 ==> tt
关于javascript - 如何访问 ui-router 的 UrlMatcher?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35632826/