.getAll(...)。然后不是一个函数

时间:2017-05-29 13:19:02

标签: javascript angularjs

我将Angular更新为1.6.4版。 所以我必须将.success.error更新为.then

现在我收到以下错误:

  

TypeError:.getAll(...)。然后不是函数

问题在于服务:

    function getAll(page, size) {
    return $http.get(baseUrl + '/jobprofiles?page='+page+'&size='+size, {timeout: 5000}).then(function (response) {
      data = response;
    }), (function(response) {
      alertService.setAlert({'message': 'jobmatch.server.unavailable', 'classified': 'danger', 'lives':1});
    });
  }

这是控制器:

    if($cookies.get("authenticated")=='true'){
    //get a list of all candidateprofiles with the use of a page and a size
    candidateprofilesService.getAll($scope.page, $scope.size).then(function() {
      $scope.data = candidateprofilesService.getData()._embedded.candidateprofiles;
      candidateprofilesService.getAll($scope.page, $scope.size+10).then(function() {
        if(candidateprofilesService.getData()._embedded.candidateprofiles.length > $scope.data.length){
          $scope.moreData = true;
        }
        else {
          $scope.moreData = false;
        }
      })
    });
  }

2 个答案:

答案 0 :(得分:1)

您的服务代码应该是:

myApp.service('candidateprofilesService', function($http) {
     this.getAll = function (page, size) {
        // just return the promise , don't evaluate here
        return $http.get(baseUrl + '/jobprofiles?page='+page+'&size='+size, {timeout: 5000});
   }    

   this.getData = function(){
      // your getData() method body, also just return the promise.
   }
});

然后在注入服务后在你的控制器中,

 candidateprofilesService.getAll($scope.page, $scope.size).then(function(response){
      //Evaluate promise here and handle the response
    }, function(error){
         //handle error
 }); 

答案 1 :(得分:0)

你的功能应该是这样的,

function getAll(page, size) {
    return $http.get(baseUrl + '/jobprofiles?page='+page+'&size='+size, {timeout: 5000}).then(function (response) {
      return data = response;
    }, function(response) {
      alertService.setAlert({'message': 'jobmatch.server.unavailable', 'classified': 'danger', 'lives':1});
    });
}