我有app/providers/weather.js
:
angular.module('forecast').provider('Weather', function() { var apiKey; this.setApiKey = function(key) { if(key) apiKey = key; }; this.$get = function() { return { ... // Some methods }; }; });
而我正试图将其注入我的config
块中:
angular.module('forecast', ['ngRoute']).config(function(WeatherProvider) { WeatherProvider.setApiKey('hello1234'); });
但是我得到:
Uncaught Error: [$injector:modulerr] Failed to instantiate module forecast due to: Error: [$injector:unpr] Unknown provider: WeatherProvider
奇怪的是,我可以成功注入Weather
控制器并可以访问这些方法this.$get
.
我的index.html
:
Forecast
我无法找出错误原因.我正在运行Angular 1.2.8.
您正在使用一个模块'forecast'
,因此在这种情况下,我们应该/必须确保配置已经可以访问提供程序.换句话说,设置这种步骤:
// I. Module creation angular.module('forecast', ['ngRoute']) // II. Provider definition .provider('Weather', function() { ... } // III. configuration using already known provider .config(function(WeatherProvider) { ... }) ;