-2
我做了一個$ http請求API返回簡單的json對象。
現在我需要以某種方式存儲這些數據,因此$ http調用將只在所有模板url上執行一次,並將粘貼到'MainController'。 什麼是最好的解決方案?
angular.module('myApp' ,['ngRoute'])
.factory('MasterData' , function($http){
var responseData;
$http.get('https://maps.googleapis.com/maps/api/geocode/json?address=1600+Amphitheatre+Parkway,+Mountain+View,+CA')
.then(function(response){
responseData = response.data;
return responseData;
})
})
.controller('MainController' , function($scope , MasterData){
})
[Caching](https://docs.angularjs.org/api/ng/service/$http#caching)? – Blackhole 2014-09-04 14:39:14
緩存是唯一的解決方案嗎? 我不能使用常量在Angular中存儲對象嗎? – 2014-09-04 14:45:16
你可以。但緩存已經爲你做了,你爲什麼要重新發明輪子? – Blackhole 2014-09-04 14:50:26