angularjs and localStorage change event

前端 未结 4 1814
梦如初夏
梦如初夏 2020-12-09 03:14

I store some data in the localStorage

what I want in my angularjs app is that when the data in the localStorage changed, the app rerender the app, how can I do this?

4条回答
  •  长情又很酷
    2020-12-09 03:43

    $scope.$on("LocalStorageModule.notification.setitem", function (key, newVal, type) {
          console.log("LocalStorageModule.notification.setitem", key, newVal, type);
        });
    
    $scope.$on("LocalStorageModule.notification.removeitem", function (key, type) {
      console.log("LocalStorageModule.notification.removeitem", key, type);
    });
    
    $scope.$on("LocalStorageModule.notification.warning", function (warning) {
      console.log("LocalStorageModule.notification.warning", warning);
    });
    
    $scope.$on("LocalStorageModule.notification.error", function (errorMessage) {
      console.log("LocalStorageModule.notification.error", errorMessage);
    });
    

    this event calling when using https://github.com/grevory/angular-local-storage#getstoragetype

    in app config

    myApp.config(function (localStorageServiceProvider) {
      localStorageServiceProvider
        .setPrefix('myApp')
        .setStorageType('sessionStorage')
        .setNotify(true, true)
    });
    

提交回复
热议问题