$injector:unpr Unknown Provider

Asked

Viewed 1,015 times

0

I am trying to create a service on Ionic 1 but am getting the following error

Error: [$injector:unpr] Unknown Provider: heartTeamSrvcProvider <- heartTeamSrvc <- teamCtrl

heartTEamSrvc code

(function) {
'use strict';

var app = angular.module('firstApp');

 app.factory('heartTeamSrvc', function($http, $scope) {

    var heartTeam = [];

    return {
        getHeartTeam: function() {
            return $http.get('json/heartTeams.json').then(function(response){
                heartTeam = response;
                return heartTeam;
            });
        }
    }
})

})

Controller

  $scope.listHeart = function(heartTeamSrvc) {
            $scope.heartTeam = getHeartTeam();
        }

What am I doing wrong?

EDITED

NEW CTRL

 function listHeartTeam(response) {
            this.heartTeam = getHeartTeam();
        }
  • You put the import on script of factory?

  • Dude, that’s right, I forgot to import it on the index. Thank you.

  • @Leonardo pera ai I will help you, rs

  • Thank you @Diogohenriquefragosodeoliv looked at the tutorial you sent me and I noticed that a lot of code that was made is not recommended in the publication rsrs

  • kkkk @Leonardo, Rlx that when I started in Angular was more lost than blind in shooting, kkkk

  • Has any response helped solve the problem and can address similar questions from other users? If so, make sure to mark the answer as accepted. To do this just click on the left side of it (below the indicator of up and down votes).

Show 1 more comment

2 answers

2

Check the import of .js in his index.html.

1

He’s saying that he doesn’t recognize the Provider for the reason that you didn’t inject the Provider(heartTeamSrvc) into the controller, possibly your Factory will break also for the reason that you didn’t inject $http or $Scope into them Follow this good practice tutorial to build your Controllers, Factorys, Services and ETC

adjust your Factory:

(function () {
'use strict';


angular.module('firstApp')
.factory('heartTeamSrvc', heartTeamSrvc)


heartTeamSrvc.$inject = ['$http', '$scope'];

function heartTeamSrvc($http, $scope) {

    var heartTeam = [];

    return {
        getHeartTeam: function() {
            return $http.get('json/heartTeams.json').then(function(response){
                heartTeam = response;
                return heartTeam;
            });
        }
    }
  }

})

Don’t forget that your controller will need adjustment too, try to do anything yourself if you can’t, add the Controller to your question and I’ll help you.

  • Oops, thank you for the reply Diogo. I imported the JS in index, I typed your code in my service and unfortunately is giving the same error I’m heartTeamSrvc import in my controller and I’m reading the tutorial you gave me, I’m a little lost but I think I’ll get.

  • @Leonardo, quiet, anything, updates in your question the Controller and Service you are editing. Try debugging Chrome, put Brack-point right at the beginning of the code, and see if it recognizes heartTeamSrvc

  • Diogo got lost a little on Ctrl, I will edit the question and insert the code q modified.

  • I guess it’s not that but it doesn’t hurt to ask right rsrs

Browser other questions tagged

You are not signed in. Login or sign up in order to post.