收到错误:未知提供商:$ urlMatcherFactory [英] Getting Error: Unkown provider: $urlMatcherFactory

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

问题描述

Unkown provider: $urlMatcherFactory 

这是当我尝试在应用程序配置注入$ urlMatcherFactory的角度抛出的错误消息。

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

我已经包括UI路由器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 
});

这个例子显示

通常code这样将无法识别 $ 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路由器你可以看到如何使用 $ 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 .

在这个例子中, $ urlMatcherFactory 中使用 .RUN()

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 (请参阅提供末)

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

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

您可能想知道为什么会有人打扰建立一个完整的供应商与提供方法,如果工厂,价值等,都容易得多。答案是,提供商允许很多的配置。我们已经提到,当你创建通过提供商的服务(或任何角度的快捷方式给你),你创建一个新的供应商,它定义了服务是如何构建。我没有提到的是,这些供应商可以注入到你的应用程序的配置部分,以便你可以与他们进行互动!

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!

首先,角运行在两个阶段的应用程序 - 的配置和运行阶段。在配置阶段,正如我们所看到的,在这里你可以设置任何供应商是必要的。这也是指令,控制器,过滤器等得到成立。运行阶段,您可能已经猜到,就是角实际上编译你的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天全站免登陆