2017-09-21 53 views
1

我在AngularJS中有兩個組件。單組分usersList包含表

<table> 
    <tbody> 
    <tr ng-repeat="..."><!--Contains code to repeat the entries in usersList localstorage object--></tr> 
    </tbody> 
</table> 

在頁面加載,該組件獲取用戶提供從$ localStorage的一個API,並將其存儲他們的詳細信息列表。 另一種形式editUserProfile其中的值進行編輯和刪除

app.component("editUserProfile", { 
    ... 
    controller: function($localStorage, ...) { 
    vm.$storage = $localStorage; 
    // Do other things 
    // Some event (edit) is successful, update the userslist in $localStorage 
    vm.$storage.usersList = response 
    } 
}) 

所以基本上,我編輯用戶的詳細信息,並通過使對API的調用刪除用戶,當一個成功的編輯/刪除發生,我刷新usersList的$ localStorage值,它是一個JSON對象。

事情總是起作用,直到更新數據,但是這些更改未反映在usersList組件中。我這樣做是爲了保存對API顯示數據的調用。歡迎任何其他想法。

+0

什麼是$ storage和$ localstorage? – Zooly

+0

我正在使用ngStorage。 $ storage和$ localStorage與ngStorage相關。 –

+0

不要忘了提及那麼:) – Zooly

回答

1

看到這個Plunkr工作。

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

app.controller('mainCtrl', function($scope, $localStorage){ 
    $scope.$storage = $localStorage; 

    $scope.$storage.entity = []; 

    $scope.add = function(){ 
    $scope.$storage.entity.push('New entry'); 
    } 

}); 

看來$localStorage$scope傳遞。

傳遞$ localStorage(或$ sessionStorage)引用到普通ol'JavaScript中的$ scope下的鉤子。

您的問題可能會附帶vm.$storage指定。

+0

這個工程!但是我通過在ngStorage值(即usersList)上使用$ watch來克服了這個問題 –