You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

执行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.js is loaded before controller.js in your index.html—Angular needs to register the service first before the controller tries to use it.
  • The module name starter is 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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 10:05:21