te')); return $arr; } /* 遍历用户所有主题 * @param $uid 用户ID * @param int $page 页数 * @param int $pagesize 每页记录条数 * @param bool $desc 排序方式 TRUE降序 FALSE升序 * @param string $key 返回的数组用那一列的值作为 key * @param array $col 查询哪些列 */ function thread_tid_find_by_uid($uid, $page = 1, $pagesize = 1000, $desc = TRUE, $key = 'tid', $col = array()) { if (empty($uid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('uid' => $uid), array('tid' => $orderby), $page, $pagesize, $key, $col); return $arr; } // 遍历栏目下tid 支持数组 $fid = array(1,2,3) function thread_tid_find_by_fid($fid, $page = 1, $pagesize = 1000, $desc = TRUE) { if (empty($fid)) return array(); $orderby = TRUE == $desc ? -1 : 1; $arr = thread_tid__find($cond = array('fid' => $fid), array('tid' => $orderby), $page, $pagesize, 'tid', array('tid', 'verify_date')); return $arr; } function thread_tid_delete($tid) { if (empty($tid)) return FALSE; $r = thread_tid__delete(array('tid' => $tid)); return $r; } function thread_tid_count() { $n = thread_tid__count(); return $n; } // 统计用户主题数 大数量下严谨使用非主键统计 function thread_uid_count($uid) { $n = thread_tid__count(array('uid' => $uid)); return $n; } // 统计栏目主题数 大数量下严谨使用非主键统计 function thread_fid_count($fid) { $n = thread_tid__count(array('fid' => $fid)); return $n; } ?>javascript - Clear $scope on logout in Angular js - Stack Overflow
最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - Clear $scope on logout in Angular js - Stack Overflow

programmeradmin3浏览0评论

In my controller I am storing data as $scope.$parent.dossierSummaries = data; but after log out and login the application $scope.$parent.dossierSummaries retains the same old data.

I am doing this on log out

.success( function( response, status ) {
    if ( response.status > 0 ) {
        var u = $rootScope.user.username;
        $cookieStore.remove('myapp');
        $rootScope.user = { username: '', role: 0 };
        success(u);
    }
    else {
        error(response.messages);
    }
})
.error( function( response, status ) {
    error(['There was an error logging you out.']);
});

In my controller I am storing data as $scope.$parent.dossierSummaries = data; but after log out and login the application $scope.$parent.dossierSummaries retains the same old data.

I am doing this on log out

.success( function( response, status ) {
    if ( response.status > 0 ) {
        var u = $rootScope.user.username;
        $cookieStore.remove('myapp');
        $rootScope.user = { username: '', role: 0 };
        success(u);
    }
    else {
        error(response.messages);
    }
})
.error( function( response, status ) {
    error(['There was an error logging you out.']);
});
Share Improve this question edited Aug 29, 2013 at 11:30 André Dion 21.7k7 gold badges58 silver badges60 bronze badges asked Aug 29, 2013 at 11:28 PrashobhPrashobh 9,54215 gold badges64 silver badges91 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 3

in angularJS, you shouldn't set the variable directly to a controller but you should retrieve it from a service instead. So whenever you load a controller you should write a init() function to get value of that model. So everytime you will have the correct data from server.

Code example and docs : http://docs.angularjs/guide/dev_guide.services.creating_services

Another approach to manually tracking and cleaning things up would be to broadcast a 'logout' event on the rootScope (or other custom event). Then listen for the event either in your controller or in your service to clean up the data.

Broadcast:

$rootScope.broadcast('logout');

Watching for an event (in a service for example):

$rootScope.on('logout',function(){
  dossiers = [];
});

I don't think there is any effective way to achieve it. Any object (controller, directive,filter or as a matter of fact any js object) can hold reference to another object (in your case user), and one cannot determine easily who all are holding reference.

The reference would only get release if you do it either explicitly or when the object holder the reference is destroyed.

What you can try is

$rootScope.user.username='';
$rootScope.role=0; 

Assuming some object are tracking this specific object the data would be cleared now.

if you don't mind a slight screen flickering on logout, you can refresh the page using a method like this:

$window.location.replace($window.location.toString().split('#')[0]);

this would clear out all the $scope and $rootScope variables, and in my case has solved the issue.

If you want to clear the $scope, I think you can use it's constructor method or proto (prototype), which is used in constructor. I believe you can do that to reset the scope to initial state. If someone knows any more on this, feel free to ment.

发布评论

评论列表(0)

  1. 暂无评论