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/