Clear service data in AngularJs

扶醉桌前 提交于 2019-12-05 10:06:05

To clear the data in myService, you may have a destroy() function that is called upon $scope.clearData. For example:

app.factory('myService',function('$http'){
   /// do you stuff
   return{
     myServices : //...,
     destroy : function(){
       promise = null;//destroy promise
     }
   }
});

On a side note, you probably don't want to store data in $scope. You may want to separate controlling and data.

I'm using angular-devise to manage my users. Here's what I have in my service to clear data on logout:

app.service("MyService", ["$rootScope", function($rootScope) {
  // destroy the user data on logout
  self = this;
  this.shouldBeDestroyed = "foo";

  $rootScope.$on('devise:logout', function(event) {
    self.shouldBeDestroyed = null;
  });
});

I'd love to find a more reliable way to destroy sensitive objects in services. This solved my problem though.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!