Angularjs $http.get().then and binding to a list

前端 未结 4 1711
借酒劲吻你
借酒劲吻你 2020-12-03 00:26

I have a list that looks like this:

  • 4条回答
    •  天涯浪人
      2020-12-03 01:21

      $http methods return a promise, which can't be iterated, so you have to attach the results to the scope variable through the callbacks:

      $scope.documents = [];
      $http.get('/Documents/DocumentsList/' + caseId)
        .then(function(result) {
          $scope.documents = result.data;
      });
      

      Now, since this defines the documents variable only after the results are fetched, you need to initialise the documents variable on scope beforehand: $scope.documents = []. Otherwise, your ng-repeat will choke.

      This way, ng-repeat will first return an empty list, because documents array is empty at first, but as soon as results are received, ng-repeat will run again because the `documents``have changed in the success callback.

      Also, you might want to alter you ng-repeat expression to:

    • because if your DisplayDocuments() function is making a call to the server, than this call will be executed many times over, due to the $digest cycles.

    提交回复
    热议问题