gpt4 book ai didi

javascript - 错误 : [$injector:undef] Provider '' must return a value from $get factory method

转载 作者:行者123 更新时间:2023-11-30 12:24:23 24 4
gpt4 key购买 nike

我正在尝试学习 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();
};

}])

感谢任何帮助...

最佳答案

将您的 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});

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

关于javascript - 错误 : [$injector:undef] Provider '' must return a value from $get factory method,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29965608/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com