在角度 js 中获取错误"then is not a function"



我正在尝试使用$http调用从文件中获取数据。我制作一个工厂并在控制器中获得结果。我想声明成功和错误方法。但我得到这个错误 accountDetailService.callAccountDetailService。then不是一个函数这是我的代码

http://plnkr.co/edit/eGaPwUdPgknwDnozMJWU?p =

预览
angular.module('ionicApp', ['ionic'])
  .controller('showhidecntr', function($scope, $window,accountDetailService) {
    $scope.hgt = $window.innerHeight / 3;
    //alert($scope.hgt)
    accountDetailService.callAccountDetailService.then(function(data){
      console.log(data);
    }).error(function(data){
       console.log("error"+data);
    })
  }).factory('accountDetailService',['$http','$q', function($http, $q) {
        return {
            callAccountDetailService: function(callback){
                $http.get('default.json').success(callback).error(callback);
            }
        };
    }])

,

promise仅在返回回调对象时有效。有两种方法可以解决这个问题。

1-将您的工厂代码更改为以下代码:

            return $http.get('default.json').success(callback).error(callback);

2-你可以只使用success而不是then,并从你的工厂中删除success和error回调。

如果由我来决定,我总是选择选项1,因为它更清楚,更容易理解。

祝你好运,玩得开心。

最新更新