未知的提供商 - AngularJS
问题描述:
我已经看到了很多类似的话题,但我没有找到解决方案。未知的提供商 - AngularJS
我得到这个错误错误:
$injector:unpr Unknown Provider
Unknown provider: restaurantsProvider <- restaurants <- restaurantsController
这里是我的控制器:
(function() {
'use strict';
angular
.module('myApp')
.controller('restaurantsController', restaurantsController);
restaurantsController.$inject = ['$scope', 'restaurants'];
function restaurantsController($scope, restaurants) {
$scope.restaurants = restaurants.query();
}
})();
和服务文件:
(function() {
'use strict';
var restaurantsService = angular.module('restaurantsService', ['ngResource']);
restaurantsService.factory('restaurantsService', ['$resource', function ($resource) {
return $resource('/api/restaurants', {}, {
query: { method: 'GET', params: {}, isArray: true }
});
}]);
})();
如果它影响到的东西,我m使用ASP.NET。
答
该错误表明您正在尝试注入角度不知道的东西。还有我发现了一些问题与您的应用程序结构,是导致这个问题...
你从来没有真正宣告你的“对myApp”模块,在这里你需要注入你的restaurantService应用。
你的控制器需要在“餐厅”的依赖,但该服务实际上是所谓的“restaurantsService”
我希望应用程序的结构看起来是这样的:
(function() {
'use strict';
var restaurantsServiceApp = angular.module('restaurantsServiceApp', ['ngResource']);
restaurantsServiceApp.factory('restaurantsService', ['$resource', function ($resource) {
return $resource('/api/restaurants', {}, {
query: { method: 'GET', params: {}, isArray: true }
});
}]);
})();
(function() {
'use strict';
var myApp = angular.module('myApp', ['restaurantsServiceApp']);
myApp.controller('restaurantsController', restaurantsController);
restaurantsController.$inject = ['$scope', 'restaurantsService'];
function restaurantsController($scope, restaurantsService) {
$scope.restaurants = restaurantsService.query();
}
})();
你serviceApp需要在被注入其他应用程序之前进行声明。
这是一个关键:“您的服务应用程序需要在被注入其他应用程序之前进行声明。”在我的app.js中,我忘了添加'restaturantsService'。 _var myApp = angular.module('myApp',['ngRoute',**'restaurantsService'**]); _ –