加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

清除AngularJs中的服务数据

发布时间:2020-12-17 18:03:43 所属栏目:安全 来源:网络整理
导读:我正在尝试将数据存储在我的服务中,类似于以下答案: Processing $http response in service app.factory('myService',function($http) { var promise; var myService = { async: function() { if ( !promise ) { // $http returns a promise,which has a th
我正在尝试将数据存储在我的服务中,类似于以下答案:

Processing $http response in service

app.factory('myService',function($http) {
  var promise;
  var myService = {
    async: function() {
      if ( !promise ) {
        // $http returns a promise,which has a then function,which also returns a promise
        promise = $http.get('test.json').then(function (response) {
          // The then function here is an opportunity to modify the response
          console.log(response);
          // The return value gets picked up by the then in the controller.
          return response.data;
        });
      }
      // Return the promise to the controller
      return promise;
    }
  };
  return myService;
});

app.controller('MainCtrl',function( myService,$scope) {
  $scope.clearData = function() {
    $scope.data = {};
  };
  $scope.getData = function() {
    // Call the async method and then do stuff with what is returned inside our own then function
    myService.async().then(function(d) {
      $scope.data = d;
    });
  };
});

但是,我注意到即使在注销后我的服务中的数据仍然存在.因此,我可以作为一个完全不同的用户登录,看到我不应该看到的数据.

注销后如何清除数据?当然,我可以手动清除所有服务中的所有内容,但我正在寻找更通用的方法,例如“清除所有用户数据”.我试图强制页面刷新,它的工作原理,但我不喜欢它产生的闪存.

编辑:示例中的代码

解决方法

要清除myService中的数据,可能有一个在$scope.clearData上调用的destroy()函数.
例如:

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

另外,您可能不希望将数据存储在$scope中.您可能希望分离控制和数据.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读