获取错误:未知提供者:$urlMatcherFactory [英] Getting Error: Unkown provider: $urlMatcherFactory

查看:25
本文介绍了获取错误:未知提供者:$urlMatcherFactory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Unkown provider: $urlMatcherFactory 

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

This is the error message that Angular throws when I try to inject $urlMatcherFactory in the app config.

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

I have included the ui-router JS file and have been using it for last few months.

我需要做这样的事情:

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

这个例子所示.

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

Normally code like this won't identify $urlMatcherFactory. So I tried injecting it as a dependency on the config but then I get this error.

推荐答案

在这个 Q&A 匹配 url 与数组中的单词列表AngularJS ui-router 你可以看到如何使用$urlMatcherFactory.还有一个指向工作plunker的链接.

In this Q&A Matching url with array list of words in AngularJS ui-router you can see how to use the $urlMatcherFactory. Also a link to working plunker .

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

In that example, the $urlMatcherFactory is used in the .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)

And that would also mean, that if you are about to use it in a config phase, you should ask for $urlMatcherFactoryProvider (see the Provider at the end)

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

BUT: using providers in a config phase means - we can configure them. I mean configure the provider itself. To be later evaluable (already configured) in run phase

您可能想知道如果工厂、值等更容易,为什么有人会费心使用提供方法来设置成熟的提供程序.答案是提供者允许大量配置.我们已经提到,当您通过提供者(或 Angular 为您提供的任何快捷方式)创建服务时,您将创建一个新的提供者,该提供者定义了该服务的构造方式.我没有提到的是,这些提供程序可以注入到应用程序的配置部分中,以便您可以与它们进行交互!

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!

首先,Angular 分两个阶段运行您的应用程序——配置阶段和运行阶段.正如我们所见,在配置阶段,您可以根据需要设置任何提供程序.这也是设置指令、控制器、过滤器等的地方.正如您可能猜到的那样,运行阶段是 Angular 实际编译您的 DOM 并启动您的应用程序的地方.

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.

这篇关于获取错误:未知提供者:$urlMatcherFactory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆