执行ionic serve遇[$injector:unpr]错误:未知Provider,服务未被识别
Fixing the [$injector:unpr] Unknown Provider Error
Hey there, I spot the issue right away—you've got a typo in your service name that's causing Angular to fail recognizing your provider! Let's break this down and fix it:
The Root Cause
In your service.js, you defined your service as ProdutosService (note the spelling: Produtos):
angular.module('starter').service('ProdutosService', ProdutosService); ProdutosService.$inject = ['$http', '$q']; function ProdutosService($http, $q){ return { lista: lista }; function lista() { return $http.get('comprar/productos.json') .then(function(response) { return response.data.productos; }) .catch(function(error){ $q.reject(); }); } };
But in your controller.js, you're trying to inject ProductosService (with an extra c: Productos):
angular.module('starter').controller('ComprarCtrl', ComprarCtrl); ComprarCtrl.$inject = ['$scope', 'ProductosService']; function ComprarCtrl($scope, ProductosService) { var vm = this; ProductosService.lista().then(function(productos) { console.log(productos); $scope.productos = productos; vm.productos = productos; }); }
Angular can't match the mismatched names, hence the "Unknown Provider" error.
Fixed Controller Code
Update your controller.js to use the correct service name ProdutosService:
angular.module('starter').controller('ComprarCtrl', ComprarCtrl); ComprarCtrl.$inject = ['$scope', 'ProdutosService']; // Corrected service name here function ComprarCtrl($scope, ProdutosService) { var vm = this; ProdutosService.lista().then(function(productos) { console.log(productos); $scope.productos = productos; vm.productos = productos; }); }
Quick Additional Checks
Just to cover all bases, double confirm:
- Your
service.jsis loaded beforecontroller.jsin yourindex.html—Angular needs to register the service first before the controller tries to use it. - The module name
starteris spelled identically in both files (you've got this right now, but it's a super common gotcha!).
Once you make this change, run ionic serve again and the error should vanish.
内容的提问来源于stack exchange,提问作者JEFFERSONALBERTO SOLANO CAMARG
相关产品推荐
相关产品推荐

