2016-07-11 7 views
1

私は私の国のリストに戻らなければならないサービスを持っています。しかし、私は国の値を変数にキャッシュして、複数のajax呼び出しを防ぐ必要があります。 私は約束オブジェクトを作成しましたが、私のHTMLはこのサービスを複数回呼び出す必要があります。私がサービスを呼び出すと、それはキャッシュから戻ってくる最初の時間からajaxから戻ってくることがあります。時にはそのキャッシュの3回後にajaxからretrunsすることもあります。これをどうすれば処理できますか?AngularJSは約束を複数回呼びます。

var vm = this; 
vm.countries; 

vm.getCountries = function() { 
    var q = $q.defer(); 
     if (vm.countries === undefined) { 
      return $http({ 
       method: 'POST', 
       cache: true, 
       url: API + '/api/Global/Countries', 
      }).then(function successCallback(response) { 
       if (errorHandler(response.data)) { 
        vm.countries = response.data; 
        q.resolve(response.data) 
        console.log("ajax") 
        return q.promise; 
       } 
      }); 
     } else { 
      console.log("cache") 
      q.resolve(vm.countries) 
      return q.promise; 
     } 
} 
+0

https://docs.angularjs.org/api/ngResource/service/

It is important to realize that invoking a $resource object method immediately returns an empty reference (object or array depending on isArray). Once the data is returned from the server the existing reference is populated with the actual data. This is a useful trick since usually the resource is assigned to a model which is then rendered by the view. Having an empty object results in no rendering, once the data arrives from the server then the object is populated with the data and the view automatically re-renders itself showing the new data. This means that in most cases one never has to write a callback function for the action methods.

リソースのドキュメントから$ HTTPの前に 'return'文を削除するようにしてください。 errorHandlerがfalseを返す場合は、エラーを処理してq.promiseを返します。 – Paqman

+0

リターンを取り除くと、 "TypeError:プロパティを読み取れません"、次に "未定義"というエラーがコンソールに表示されます。 – Sam

答えて

2

があなたの代わりにデータの約束をキャッシュすることができます

は、ここに私のサービスです。 また、ここでのエラーの場合に何が起こるかを検討することもできます。キャッシュされた約束をクリアする必要があります。あなたがテンプレートに値を結合するためにそれを使用している場合は、すべての状況

vm.getCountries = function() { 
    var q = $q.defer(); 
     if (vm.countries === undefined) { 
      return $http({ 
       method: 'POST', 
       cache: true, 
       url: API + '/api/Global/Countries', 
      }).then(function successCallback(response) { 
       if (errorHandler(response.data)) { 
        vm.countries = response.data; 
        q.resolve(response.data) 
        console.log("ajax") 

       } 
      }); 
     } else { 
      console.log("cache") 
      q.resolve(vm.countries) 
     } 

     return q.promise; 
} 
+0

それから、エラー機能を追加します。ありがとうございます! – Sam

+0

解決策を改善する必要があります。私の他の質問もチェックしていただけますか? http://stackoverflow.com/questions/38308915/angularjs-handle-calling-promise-multiple-times-with-some-exceptions – Sam

3

戻りq.promise。私は、$resourceを使っても達成できると思います。

angular.module('myApp').factory('Country', function($resource) { 
    return $resource(API + '/api/Global/Countries'); 
}); 

//in controller 
var countries= Country.query(function() { 
    console.log(countries); 
}); 

引用$リソース

https://www.sitepoint.com/creating-crud-app-minutes-angulars-resource/

+0

動作しますか? vm.getCountries()を2回呼び出すとどうなりますか?つまり、$ http要求を解決する前に同じ関数を呼び出すとします。 –

1

var vm = this; 

function getCountries() { 
    if (!vm.countryPromise) { 
     vm.countryPromise = $http({ 
      method: 'POST', 
      cache: true, 
      url: API + '/api/Global/Countries', 
     }).then(function successCallback(response) { 
      if (errorHandler(response.data)) { 
       console.log("ajax") 
       return response.data; 
      } 
     }); 
    } else { 
     console.log("cache") 
    } 
    return vm.countryPromise; 
} 
関連する問題