javascript - .getAll(...).then 不是一个函数

标签 javascript angularjs

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

现在我收到以下错误:

TypeError: .getAll(...).then is not a function

问题出在服务中:

    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});
    });
  }

这是 Controller :

    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;
        }
      })
    });
  }

最佳答案

您的服务代码应该是这样的:

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.
   }
});

然后在注入(inject)服务后在 Controller 中,

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

关于javascript - .getAll(...).then 不是一个函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44243529/

相关文章:

javascript - 在同一索引中将公共(public)数组元素对齐在一起

javascript - 如何使用 JSON 中的 ng-repeat 填充 <li> 中的值

javascript - 带有 angularjs 的表单不会填写所有字段

javascript - 将带有 Redux 组件的 React 附加到 iframe

javascript - 检测鼠标悬停在 iframe 上?

javascript - Highcharts - 关于完整图表宽度的问题

regex - 如何在绑定(bind)表达式中使用正则表达式文字?

javascript - 如何使用 Angular.js 展开和折叠 div

angularjs - 如何使用一个输入控件应用多个 ng-pattern

javascript - "All browser compatibility"和 "Cross browser compatibility"和有什么区别?