2016-02-25 38 views
0

ui-router的文档中有一个名为UrlMatcher的对象,我需要访问它才能使用某些方法(例如exec),但我找不到任何有关如何执行此操作的明确说明。但是,该对象在文档页面中进行了描述。我如何访问ui路由器的UrlMatcher?

我希望能够做一些事情,如:

new UrlMatcher('/user/{id}?q&r').exec('/user/bob', { 
    x: '1', q: 'hello' 
}); 

我怎样才能访问该对象?

urlMatcherFactory.js on Github

回答

1

角有$urlMatcherFactory and UrlMatchers

Plunker: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

使用EXEC:

网址: /家/ 1参数1 = TT

var urlMatcher = $urlMatcherFactory.compile("/home/:id?param1"); 
    var matched = urlMatcher.exec($location.path(), $location.search()); 

你得到2 fieldsID ==> 1参数1 ==> tt

+0

我看过这篇文章,但它不直接访问'UrlMatcher'对象 – CodeArtist

+0

apparantly,var urlMatcher = $ urlMatcherFactory.compile(“'/ user/{id}?q &r'"); var matched = urlMatcher.exec('/ user/bob',{ x:'1',q:'hello' });这是你的意思吗? –

+1

是的,我要去尝试一下。 – CodeArtist