hallo aus deutschland
hallo aus deutschland

Reputation: 35

Error: [$injector:undef] Provider '' must return a value from $get factory method

I am trying to learn AngularJS. Whdile I was following the instructor, I wrote the same code as he did. But I am getting a Error: [$injector:undef] Provider 'eliteApi' must return a value from $get factory method. . When I searched for this error in web, it is told that I must return a function or an object. I guess I am doing it. My factory declaration is below:

angular.module("eliteApp",["ionic"])
.factory('eliteApi', function() {

  function eliteApi(){

    var leagues = JSON.parse('"name": "Spring Fling Tournament 2014","id": 2009,"homeScreen": "* 5th Grade Championship - 7:30m MAC 3\n* 6th Grade Championship - 6:30pm MAC 4\n* 7th Grade White Championship - 7:30pm MAC 4\n* 7th Grade Green Championship - 7:30pm MAC 2\n* 8th Grade Championship - 7:30pm MAC 1\n* 9th Grade Championship - 8:30pm MAC 4\n* 10th Grade Championship - 8:30pm MAC 1\n* 11th Grade Championship - 8:30pm MAC 3","rulesScreen": null');


      function getLeagues(){
        return leagues;
      }



    return {
      getLeagues : getLeagues 
   };
  };
  
})

.controller('LeaguesCtrl', [ '$scope','eliteApi', function($scope, eliteApi) {



  function LeaguesCtrl(eliteApi){
    var vm = this ;
    var leagues = eliteApi.getLeagues();
    };

}])

Any helps are appreciated...

Upvotes: 1

Views: 7377

Answers (2)

hallo aus deutschland
hallo aus deutschland

Reputation: 35

@Sourabh- thank you for ur help, this is the solution:

.factory('eliteApi', function() {
 var leagues1 = JSON.stringify({"name": "Spring Fling Tournament 2014","id": 2009,"homeScreen": "* 5th Grade Championship - 7:30m MAC 3\n* 6th Grade Championship - 6:30pm MAC 4\n* 7th Grade White Championship - 7:30pm MAC 4\n* 7th Grade Green Championship - 7:30pm MAC 2\n* 8th Grade Championship - 7:30pm MAC 1\n* 9th Grade Championship - 8:30pm MAC 4\n* 10th Grade Championship - 8:30pm MAC 1\n* 11th Grade Championship - 8:30pm MAC 3","rulesScreen": null});
var leagues = JSON.parse(leagues1);

    return {
      getLeagues :    function getLeagues(){     
   return leagues;
        }
    };
  })
  
 .controller('LeaguesCtrl', [ '$scope','eliteApi', function($scope, eliteApi) {

    var leagues = eliteApi.getLeagues();
       var leagueData = eliteApi.getLeaguesData();

  
})

Upvotes: 1

Zee
Zee

Reputation: 8488

Change your factory to this:

.factory('eliteApi', [function() {

 return {
          getLeagues : function(){
                           var leagues = JSON.parse({"name": "Spring Fling Tournament 2014","id": 2009,"homeScreen": "* 5th Grade Championship - 7:30m MAC 3\n* 6th Grade Championship - 6:30pm MAC 4\n* 7th Grade White Championship - 7:30pm MAC 4\n* 7th Grade Green Championship - 7:30pm MAC 2\n* 8th Grade Championship - 7:30pm MAC 1\n* 9th Grade Championship - 8:30pm MAC 4\n* 10th Grade Championship - 8:30pm MAC 1\n* 11th Grade Championship - 8:30pm MAC 3","rulesScreen": null});
                           return leagues;
                       }
        }
}])

See if this works.

UPDATE

your variable league is not a proper JSON. Change it to

 var leagues = JSON.parse({"name": "Spring Fling Tournament 2014","id": 2009,"homeScreen": "* 5th Grade Championship - 7:30m MAC 3\n* 6th Grade Championship - 6:30pm MAC 4\n* 7th Grade White Championship - 7:30pm MAC 4\n* 7th Grade Green Championship - 7:30pm MAC 2\n* 8th Grade Championship - 7:30pm MAC 1\n* 9th Grade Championship - 8:30pm MAC 4\n* 10th Grade Championship - 8:30pm MAC 1\n* 11th Grade Championship - 8:30pm MAC 3","rulesScreen": null});

You forgot the {}. Also remove the quotes'.

Upvotes: 2

Related Questions