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

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

I have a list that looks like this:

  • 4条回答
    •  盖世英雄少女心
      2020-12-03 01:25

      Promise returned from $http can not be binded directly (I dont exactly know why). I'm using wrapping service that works perfectly for me:

      .factory('DocumentsList', function($http, $q){
          var d = $q.defer();
          $http.get('/DocumentsList').success(function(data){
              d.resolve(data);
          });
          return d.promise;
      });
      

      and bind to it in controller:

      function Ctrl($scope, DocumentsList) {
          $scope.Documents = DocumentsList;
          ...
      }
      

      UPDATE!:

      In Angular 1.2 auto-unwrap promises was removed. See http://docs.angularjs.org/guide/migration#templates-no-longer-automatically-unwrap-promises

    提交回复
    热议问题