2016-12-05 52 views
1

我有一個angular model命名行。本地草料和角度 - 從本地存儲獲取項目的問題

我有一段代碼試圖使用LocalForage從本地存儲中獲取數據,並將本地數據設置爲行模型。

顯然,在本地存儲回調獲取項目執行時,$scope.rows模型超出了範圍。

有沒有辦法,我可以做到這一點,即從本地存儲設置值到行模型?

localforage.getItem(localBlogKey, function (err, readValue) 
    { 
    if (readValue !== null && readValue !== undefined) 
     { 
      var localdata = readValue.data;      
      $scope.rows = angular.fromJson(localData); 
     } 
    }); 

由於這段代碼是異步運行的,$scope.rows超出了作用域?是否有一種同步方式從localforage中提取數據?

+0

使用此https://docs.angularjs.org/api/ng/service/$q –

回答

0

這是使用ngStorage模塊實現的。 導入ngStorage模塊,然後用鑰匙訪問localStorage的項目如下

localstorage.myKey = "myValue"; 

這是同步的,因此我的問題解決了。