javascript - 如何访问 ui-router 的 UrlMatcher?

标签 javascript angularjs angular-ui-router pattern-matching

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

我希望能够做这样的事情:

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

我如何访问该对象?

urlMatcherFactory.js on Github

最佳答案

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 ==> 1param1 ==> tt

关于javascript - 如何访问 ui-router 的 UrlMatcher?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35632826/

相关文章:

javascript - Angularjs $interval.flush()

javascript - 用于导航用户的 Angular 三元运算符

javascript - ng-model 不会在文本过滤器的单个按键上重新计算数组长度

angularjs - 无法从状态 '...' 解析 ''

javascript - AngularJs:相对路径不使用 $location.path 重定向

javascript - 如何在 jQuery 选择器中执行 Javascript

javascript - 这些回调如何适应配置对象?

javascript - Jquery .each() 无限循环

javascript - 指令在 ng-repeat 中不起作用

html - Angularjs UI-路由器问题