我正在尝试通过我的工厂中的 $http 调用从 JSON 文件中加载数据,但每次运行代码时,我都会遇到同样的错误。如何修复这个问题。
错误
TypeError: undefined is not a function
at Object.getFruitsData (http://localhost/test/JSON/js/controllers.js:12:18)
at new <anonymous> (http://localhost/test/JSON/js/controllers.js:3:16)
at invoke (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:3869:17)
at Object.instantiate (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:3880:23)
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:7134:28
at http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:6538:34
at forEach (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:330:20)
at nodeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:6525:11)
at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:5986:15)
at compositeLinkFn (http://ajax.googleapis.com/ajax/libs/angularjs/1.2.16/angular.js:5989:13)
下面是我正在使用的代码。
fruitsFactory.js
app.factory('fruitsData', function($http, $log){
return{
getFruitsData: function(succescb){
$http({method:'GET', url:'json/testList.json'})
.succes(function(data){
succescb(data);
})
.error(function(data){
$log.warn(data);
});
}
};
});
controller.js
app.controller('fruitsController',['$scope','fruitsData', function($scope, fruitsData){
fruitsData.getFruitsData(function(fruits){
$scope.fruits = fruits;
});
}]);