错误:[$ injector:undef] Provider''必须从$ get factory方法返回一个值

时间:2015-04-30 10:43:10

标签: javascript angularjs ionic

我正在努力学习AngularJS。在我跟随导师的时候,我写了与他相同的代码。但我得到Error: [$injector:undef] Provider 'eliteApi' must return a value from $get factory method.。当我在网络中搜索此错误时,会告诉我必须返回一个函数或一个对象。我想我在做。我的工厂声明如下:

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();
    };

}])

感谢任何帮助...

2 个答案:

答案 0 :(得分:2)

将您的factory更改为:

.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;
                       }
        }
}])

看看是否有效。

<强>更新

您的变量league不合适JSON。将其更改为

 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});

你忘记了{}。同时删除引号'

答案 1 :(得分:1)

@ Sourabh- 谢谢你的帮助, 这是解决方案:

.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();

  
})