Angularjs $ localstorage在其他选项卡中不起作用



我试图在一个选项卡中发生更改时执行fuction,更改也应在另一个选项卡中反映。

我写这篇文章,它在同一选项卡中工作正常,即使我手动浏览localStorage,也可以更改它,并确保它的工作正常。

但是问题,更改也不能在其他选项卡中反映。

我的意思是,我打开了两个选项卡,当我在一个选项卡中更改某些内容时,在另一个选项卡中应该反映。

每当在一个选项卡中发生任何更改时,第二个选项卡应该执行此$scope.getAllContact();并等待另一个更改,如果发生另一个更改,则应执行$scope.getAllContact();

$localStorage.editedData = response.data;
      $scope.editedID = $localStorage.editedData.id;
      if (response.data.id == $localStorage.editedData.id) {
        $localStorage.isChanged = true;
      }
      while ($localStorage.isChanged == true) {
        $scope.getAllContact();
        break        
      }

我写上述代码;

如果您在几行上方进行,则忽略下面的代码,下面的代码为fuction,当发生更改时,应执行fuction:

$scope.getAllContact = function() {
    var data = $http.get("http://127.0.0.1:8000/api/v1/contact")
    .then(function(response) {
      $scope.contacts = response.data;
      // below two line of codes will reload in every 10 millisecond if a request succcess
      // in result, you will see refleation in other browser if any change occurs in another browser
      // do nothing for now
    }, function(response) {
      //$scope.connectionError = "Opps ! Having Trouble in loading this page ! n  It's Connection Error !!!";
      // below two line as as above two commented line
      // do nothing for nw
    });
  };
  $scope.getAllContact();

您需要在特定时间差检查本地存储

 setInterval(function(){
     if ($localStorage.isChanged == true) {
            $scope.getAllContact();      
          }  
          }
    }, 500);

请参见下面的代码可能有帮助

$scope.$apply(function() {
    $scope.contacts = response.data;
});

根据我的假设,您只能看到一个选项卡。因此,致电$scope.getAllContact()每当您单击第二个选项卡时。

最新更新