Angularjs在json Temp Variabel中添加新记录

时间:2015-06-03 06:33:03

标签: javascript json angularjs

我获取当前用户ID并从数据库中获取一些详细信息。

$http.post('get-family-list.php', {user_id:user_id}).
    success(function(data, status) {
      $localStorage.family = data;
    }).
    error(function(data, status) {
      console.log('Cannot Retrieve Family List');  
  });

此代码有效,然后用户可以在同一视图中添加新记录。我的意思是CRUD操作都在同一页面中,我在app基本控制器中调用上面的代码。不在CRUD页面中。

在CRUD控制器中..

$scope.family_list = $localStorage.family;

所以现在我的问题是当用户创建新记录或编辑记录时,我无法更新列表..我不想再次从服务器获取整个列表。我想将新值添加/更新到本地存储变量$ localStorage.family

我在插入/更新http请求的成功函数中编写了这段代码,它无法正常工作。

$scope.family_list[data.id] = {"nric":this.nric,"dob":date_of_birth,"name":this.name,"gender":this.gender,"relationship":this.relationship,"student":this.student};

$localStorage.family = $scope.family_list;

和我的json值

{"4":{"ID":"4","UID":"26","nric":"sdf","dob":"01\/01\/1990","name":"a","gender":"1","relationship":"1","student":"1","created":"2015-06-03 11:04:29"},"5":{"ID":"5","UID":"26","nric":"aaaa","dob":"01\/01\/1990","name":"aaa","gender":"1","relationship":"1","student":"1","created":"2015-06-03 11:13:21"},"1":{"ID":"1","UID":"26","nric":"ad","dob":"17\/05\/1990","name":"asdf asf","gender":"1","relationship":"1","student":"1","created":"2015-06-02 21:33:59"},"2":{"ID":"2","UID":"26","nric":"ad new","dob":"24\/03\/1984","name":"asdf asf new","gender":"2","relationship":"2","student":"2","created":"2015-06-02 21:46:50"},"3":{"ID":"3","UID":"26","nric":"insert 113","dob":"01\/01\/1990","name":"insert update","gender":"1","relationship":"1","student":"1","created":"2015-06-03 10:41:23"},"6":{"ID":"6","UID":"26","nric":"adfsdf","dob":"01\/01\/1990","name":"sdfsdf","gender":"1","relationship":"1","student":"1","created":"2015-06-03 11:14:20"}}

感谢所有

0 个答案:

没有答案