SORU
20 EYLÜL 2012, PERŞEMBE


İşleme hizmetinde $http yanıtı

Geçenlerde here en ÇOK karşı karşıyayım sorunun ayrıntılı bir açıklama gönderildi. $http gerçek bir istek gönderemedim olarak, zaman uyumsuz davranış simülasyonu için zaman aşımı kullandım. Veri görünümü için benim model bağlama doğru, @Gloopy yardımı ile çalışıyor

$timeout yerine $http kullandığımda şimdi, bu yerel test), zaman uyumsuz bir istek başarılı olduğunu görebiliyordum ve data benim hizmet json yanıt ile doludur. Ama, benim görüşüme güncelleme değil.

güncelleştirilmiş Plunkr here

CEVAP
20 EYLÜL 2012, PERŞEMBE


Burada istediğiniz özellikleri içeren bir Çarpması vardır: http://plnkr.co/edit/TTlbSv?p=preview

Fikri vaat doğrudan ve "sonra" ve zaman uyumsuz olarak iade yanıtları işlemek erişmek için fonksiyonlar. çalışmak.

app.factory('myService', function($http) {
  var myService = {
    async: function() {
      // $http returns a promise, which has a then function, which also returns a promise
      var promise = $http.get('test.json').then(function (response) {
        // The then function here is an opportunity to modify the response
        console.log(response);
        // The return value gets picked up by the then in the controller.
        return response.data;
      });
      // Return the promise to the controller
      return promise;
    }
  };
  return myService;
});

app.controller('MainCtrl', function( myService,$scope) {
  // Call the async method and then do stuff with what is returned inside our own then function
  myService.async().then(function(d) {
    $scope.data = d;
  });
});

Burada sadece ilk süre isteği önbelleğe biraz daha karmaşık bir sürümü (http://plnkr.co/edit/2yH1F4IMZlMS8QsV9rHv?p=preview):

app.factory('myService', function($http) {
  var promise;
  var myService = {
    async: function() {
      if ( !promise ) {
        // $http returns a promise, which has a then function, which also returns a promise
        promise = $http.get('test.json').then(function (response) {
          // The then function here is an opportunity to modify the response
          console.log(response);
          // The return value gets picked up by the then in the controller.
          return response.data;
        });
      }
      // Return the promise to the controller
      return promise;
    }
  };
  return myService;
});

app.controller('MainCtrl', function( myService,$scope) {
  $scope.clearData = function() {
    $scope.data = {};
  };
  $scope.getData = function() {
    // Call the async method and then do stuff with what is returned inside our own then function
    myService.async().then(function(d) {
      $scope.data = d;
    });
  };
});

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Eric Anthony

    Eric Anthony

    13 AĞUSTOS 2011
  • njhaley

    njhaley

    24 NİSAN 2006
  • Soulkiller13 ツ

    Soulkiller13

    30 Mayıs 2013