我试图通过 Angular 工厂从远程服务器获取 JSON。但是当我从服务调用 get 方法时,出现错误:
TypeError: Events.getEvents is not a function
我的工厂代码:
angular.module('starter.services', []).config(function($httpProvider) {
delete $httpProvider.defaults.headers.common['X-Requested-With'];})
.factory('Events', function($http) {
return {
getEvents : function() {
return $http({
url: 'http://cronicls.ru/events',
method: 'GET',
params: { startDate: '2015-09-03', endDate: '2015-09-18' }
});
},
};
});
Controller 代码:
.controller('EventListCtrl', function($scope, $http, Events) {
Events.getEvents().success(function(data){
console.log(data);
})
}
);
出了什么问题?
最佳答案
尝试删除 .factory 末尾的分号,并确保您的 Controller 位于工厂后面,就像您在 .config 之后处理 .factory 一样
关于javascript - Angular 工厂不返回方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32812124/