gpt4 book ai didi

javascript - Angular 工厂不返回方法

转载 作者:行者123 更新时间:2023-11-28 06:52:44 24 4
gpt4 key购买 nike

我试图通过 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/

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