【问题标题】:Redirect a user to a specific state after a certain amount of time在一定时间后将用户重定向到特定状态
【发布时间】:2016-08-27 12:11:30
【问题描述】:

我有一个服务,它给了我用户可以登录的毫秒数,SecurityServices.getUserLoginDuration(),这个持续时间显示在所有页面的顶部,所以我写了一个显示这个时间的指令。

<session-timeout value="60000"></session-timeout>

 <session-timeout value="$root.timeoutValue"></session-timeout>

我使用这段代码在特定时间过后将用户重定向到登录页面:

$scope.timeout = $scope.timeoutValue;

    $interval(decrementBySecond, 1000);

    $timeout(emitSessionTimeout, $scope.timeoutValue);

    function decrementBySecond() {
        $scope.timeout = $scope.timeout - 1000;           
    }

    function emitSessionTimeout() {
        $rootScope.$emit('SESSION_TIMEOUT');
    }

在我的应用程序的run 块中,我处理SESSION_TIMEOUT 事件,如下所示:

 app.run(handleTimeOut);

 function handleTimeout($rootScope, $state) {
        $rootScope.$on('SESSION_TIMEOUT', function () {
            $state.go('login');
        });
    }

应该在每次状态更改时刷新超时值,因此指令控制器中的$scope.timeout 应该初始化为其初始值。我怎样才能做到这一点?

【问题讨论】:

  • 我认为这样的事情不应该在前端完成。在后端为您的会话设置过期日期,如果调用 API 时会话过期,则返回 401 UNAUTHORIZED 状态。
  • @JahongirRahmonov 服务器端逻辑尚未开发,所以我需要一些方法来模拟或模仿这种行为,你所说的可以在拦截器的帮助下轻松完成,我知道这一点.. .

标签: angularjs session events angular-ui-router timeout


【解决方案1】:

我对我的指令进行了一些修改,在双向数据绑定的帮助下,我得到了我想要的结果:

这里是修改:

$scope.timeout = angular.copy($scope.timeoutValue);

$interval(decrementBySecond, 1000);

$timeout(emitSessionTimeout, $scope.timeout);

function decrementBySecond() {
    $scope.timeoutValue= $scope.timeoutValue- 1000;           
}

function emitSessionTimeout() {
    $rootScope.$emit('SESSION_TIMEOUT');
}

我从指令隔离范围和使用指令的范围的双向数据绑定中收到timeoutValue$rootScope.timeoutValue 被每个状态转换更改,因此每次状态更改都会更新此值并反映其新值in 指令,指令的变化也反映在视图中。

【讨论】:

    猜你喜欢
    • 2016-08-24
    • 2013-03-03
    • 1970-01-01
    • 2016-07-15
    • 1970-01-01
    • 2021-01-22
    • 1970-01-01
    • 2018-09-21
    • 1970-01-01
    相关资源
    最近更新 更多