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...
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 '
.
@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(); })
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.