javascript - 出现错误 : Unkown provider: $urlMatcherFactory

标签 javascript angularjs angular-ui-router

Unkown provider: $urlMatcherFactory 

这是当我尝试在应用程序配置中注入(inject) $urlMatcherFactory 时 Angular 抛出的错误消息。

我已经包含了 ui-router JS 文件,并且在过去的几个月里一直在使用它。

我需要做这样的事情:

var dateMatcher = $urlMatcherFactory.compile("/day/{start:date}");
$stateProvider.state('calendar.day', {
    url: dateMatcher 
});

this example显示。

通常这样的代码不会识别$urlMatcherFactory。因此,我尝试将其作为对配置的依赖项注入(inject),但随后出现此错误。

最佳答案

在此问答中Matching url with array list of words in AngularJS ui-router您可以看到如何使用$urlMatcherFactory。还有 working plunker 的链接.

在该示例中,$urlMatcherFactory 用于 .run():

  .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'
        });
    }
  ]);

这也意味着,如果您要在配置阶段使用它,您应该要求 $urlMatcherFactoryProvider(请参阅最后的Provider)

但是:在配置阶段使用提供程序意味着 - 我们可以配置它们。我的意思是配置提供者本身。稍后在运行阶段可评估(已配置)

Configuring Providers

You may be wondering why anyone would bother to set up a full-fledged provider with the provide method if factory, value, etc. are so much easier. The answer is that providers allow a lot of configuration. We've already mentioned that when you create a service via the provider (or any of the shortcuts Angular gives you), you create a new provider that defines how that service is constructed. What I didn't mention is that these providers can be injected into config sections of your application so you can interact with them!

First, Angular runs your application in two-phases--the config and run phases. The config phase, as we've seen, is where you can set up any providers as necessary. This is also where directives, controllers, filters, and the like get set up. The run phase, as you might guess, is where Angular actually compiles your DOM and starts up your app.

关于javascript - 出现错误 : Unkown provider: $urlMatcherFactory,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27165537/

相关文章:

javascript - 在输入时验证 html 文本输入

javascript - Javascript 对象中的函数数组

javascript - 当我更改 js 文件时 gulp js 给出了错误..| throw 者

javascript - 使用 ionic /用户界面路由解决 promise

angularjs - 如何使用 ui-router 在运行时 Hook 新的子状态?

javascript - 如何使用react中的setInterval方法来处理react中的事件?

javascript - 如何根据指令中的属性设置添加附加功能?

javascript - ng-bind-html 将数据呈现为 HTML 但忽略子元素

javascript - 使用 UI-Router 避免在 URL 更改时重新加载页面

javascript - 为什么 uploadcare 每 N 毫秒重绘 html 标签?