javascript - Angular 工厂不返回方法

标签 javascript angularjs

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

相关文章:

javascript - 如何连接数组中对象的两个属性以用作 ngOptions 中的标签

angularjs - 为什么我们不能添加 $scope 作为 angular.service 的参数?

javascript - 在 Controller 中调用模态函数 - AngularJS

javascript - 如何防止一个用户脚本中附加的事件处理程序干扰另一用户脚本?

javascript - 如何模糊 HTML5 视频标签的特定区域?

JavaScript/jQuery : does anybody already created a loading fixed div on top 100%:100% of the page with jquery?

javascript - angularjs:ng-repeat 中的自定义过滤器

javascript - 如何获取 Bootstrap slider 的最小值和最大值输入

javascript - 每次加载时按内容设置 iframe 高度

javascript - 在 AngularJS 中更改 Canvas 的边框颜色