当AngularJS在localStorage中获取新数据时调用方法 [英] AngularJS calling a method when its get new data in localStorage

查看:73
本文介绍了当AngularJS在localStorage中获取新数据时调用方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用angularJS localstorage,并很好地注入了代码,但是出现了问题.

I am using angularJS localstorage and i injected this very well but problem in code.

我想在本地存储获取新数据时,使其自动调用函数 $ scope.getAllContact().

I want when localstorage gets new data, so that its call a function $scope.getAllContact() automatically.

我正在尝试解决此问题,例如,我在浏览器中打开了两个选项卡,如果我在一个选项卡中进行了更改,则最新更改也应反映在其他选项卡中,而无需重新加载和刷新.

I am trying to solve this issue coz, for example, i opened two tab in browser, if i change anything in one tab, the latest change should reflect in other tab too without any reload and refresh.

首先看到我的代码:

app.controller('crudCtrl', function($scope, $http, $timeout, $localStorage, $sessionStorage) {

  $scope.getAllContact = function() {
    var data = $http.get("http://127.0.0.1:8000/api/v1/contact/")
    .then(function(response) {
      $scope.contacts = response.data;
      // show something if success
    }, function(response) {
      //show something error
    });
  };
  $scope.getAllContact();

  // below method will post data
  $scope.formModel = {}; 
  $scope.onSubmit = function () {
      $http.post('http://127.0.0.1:8000/api/v1/contact/', $scope.formModel)
      .then(function(response) {

        $localStorage.name = response.data;
        $timeout(function() {

          $scope.successPost = '';
        }, 4000);
        //below $scope will push data in client site if the request is success
        $scope.contacts.push(response.data);
        //if any error occurs, below function will execute
      }, function(response) {
        // do nothing for now
      });
  };
});

$ scope.onSubmit()方法的上方,我也将提交的数据发送到localstorage,所以我需要,如果localstorage获取新数据,它应该执行 $ scope.getAllContact()始终自动进行,无需任何刷新.

above in $scope.onSubmit() methond, i send the submitted data to localstorage too, so i need if localstorage gets new data, it should execute $scope.getAllContact() always automatically without any refresh.

有人可以解决我这个问题吗?

Can anyone fix me this issue?

推荐答案

使用

有关信息,请参见

这篇关于当AngularJS在localStorage中获取新数据时调用方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆