2017-09-21 93 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来克服了这个问题 –