我有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
注入到控制器中d有权访问this.$get
中的方法。
我index.html
:
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Forecast</title>
</head>
<body ng-app="forecast">
<section class="content" ng-view></section>
<script src="bower_components/angular/angular.js"></script>
<script src="bower_components/angular-route/angular-route.js"></script>
<script src="app/app.js"></script>
<script src="app/controllers/home.js"></script>
<script src="app/controllers/settings.js"></script>
<script src="app/providers/weather.js"></script>
</body>
</html>
我无法找出错误的原因。我正在运行Angular 1.2.8。