2016-02-25 39 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