2015-01-09 23 views
0

在加載控制器之前,我試圖想出一個調用來解析大量數據對象。 我正在使用ui-router,並且非常瞭解如何解決問題,但是如果它們不在本地存儲中,我只需要加載api對象。下面是一些代碼段我已經到位通過本地存儲或api調用解決所有數據

//數據模型,保存所有對象一旦解決所有加載從本地存儲。

function TraqMetaDataModel() { 

return { 
    countries: [], 
    brands: [], 
}; 

TraqMetaDataLoader.load() 

var traqMetaDataService = { 
    load : load 
}; 

return traqMetaDataService; 

function load() { 
    CountryLoader.fetchCountries(countrySuccess); 
    BrandLoader.fetchBrands(brandSuccess); 
} 

function countrySuccess(response) { 

    angular.forEach(LocalStorageHelper.get('countries'),function(value){ 
    TraqMetaDataModel.countries.push({CountryId : value.CountryId, Country : value.Country}); 
    }); 

} 

function brandSuccess(response) { 
    angular.forEach(LocalStorageHelper.get('brands'),function(value){ 
    TraqMetaDataModel.brands.push({BrandId : value.BrandId, BrandName : value.BrandName}); 
    }); 

} 

我有所有的邏輯來看看該項目是否存在localstorage,如果沒有從api調用中獲得。我想即時通訊尋找一種方法,在解析函數中檢查應用程序狀態是否準備就緒......

我將在ui路由中使用抽象路由進行單個調用。

有興趣看看有沒有人做過這樣的事情。

謝謝羅布

+0

您是否希望等到數據被加載,然後繼續? – 2015-01-09 19:48:31

+0

我正在尋找模型來填充本地存儲的數據或必要的API調用 – 2015-01-09 19:53:42

回答

0

這樣的事情? 如果你沒有找到在本地存儲數據,然後使用它拿來$http

var app = angular.module('plunker', []); 

app.controller('MainCtrl', ['$scope','$http', function($scope, $http) { 
    $scope.name = 'World'; 
    $scope.data = ["Data1","Data2","Data3","Data4"] 

    angular.forEach($scope.data, function(obj, i){ 
    $http({method:'GET', url: 'http://httpbin.org/get', headers: { 
    'Content-Type': "application/json" 
}}). 
     success(function(data, status) { 
      $scope.status = status; 
      $scope.data = data; 
     }). 
     error(function(data, status) { 
      $scope.data = data || "Request failed"; 
      $scope.status = status; 
     }); 
    }); 
}]); 

參見:http://plnkr.co/edit/cNDULiN02i430zl1zMpp?p=info

+0

謝謝,但我已經處理了查找數據並使用$ http獲取數據。我的問題是試圖在控制器解決之前找到所有可用的數據。我不想要的是每組數據的所有單獨的決議。 – 2015-01-09 23:18:44

+0

你不能在這種情況下使用服務嗎?服務是單例,並在控制器之前初始化。 – 2015-01-10 03:19:33