为自定义指令(ngIdle)实现配置的更好方法?
Better way of implementing config for a custom directive (ngIdle)?
对于那些不熟悉的人,可以找到 ngIdle here。
这基本上是一种空闲超时的方法,就像许多银行在其网站上使用 angular。
它工作得很好,但是我目前使用它的方式是在我的控制器中放置一些配置内容。问题是我正在使用多个控制器并且真的不想在我的项目中复制粘贴这些配置内容。
配置如下:
function closeModals()
{
// Function closes any modals that are currently open (used when coming back from idle)
if ($scope.warning) {
$scope.warning.close();
$scope.warning = null;
}
if ($scope.timedout) {
$scope.timedout.close();
$scope.timedout = null;
}
}
$scope.$on('IdleStart', function () { // What happens when the user goes idle (idle time is defined in app.js IdleProvider config)
closeModals();
$scope.warning = $modal.open({
templateUrl: 'views/timeoutModal.html',
windowClass: 'modal-danger'
});
});
$scope.$on('IdleTimeout', function () { // This is what happens when the user waits passed the warning timer
logout();
closeModals();
Idle.unwatch();
$scope.$apply();
alert("You have been signed out due to inactivity.");
});
$scope.$on('IdleEnd', function () { // What happens when the user comes back from being idle but before they are timed out
closeModals();
$scope.amIdle = false;
});
基本上,我希望能够简单地告诉我的控制器我想使用这些配置设置,它无需将它们放入控制器就可以使用它们。
你不应该在多个地方需要这个配置。查看 NgIdle,他们正在从 $rootScope 广播事件。 https://github.com/HackedByChinese/ng-idle/blob/develop/angular-idle.js#L193
您需要在不同的范围内设置 watch 的唯一原因是您想要为特定事件执行多项操作。
您需要做的就是在单个控制器中注入 $rootScope 并将事件观察器添加到其中。
对于那些不熟悉的人,可以找到 ngIdle here。
这基本上是一种空闲超时的方法,就像许多银行在其网站上使用 angular。
它工作得很好,但是我目前使用它的方式是在我的控制器中放置一些配置内容。问题是我正在使用多个控制器并且真的不想在我的项目中复制粘贴这些配置内容。
配置如下:
function closeModals()
{
// Function closes any modals that are currently open (used when coming back from idle)
if ($scope.warning) {
$scope.warning.close();
$scope.warning = null;
}
if ($scope.timedout) {
$scope.timedout.close();
$scope.timedout = null;
}
}
$scope.$on('IdleStart', function () { // What happens when the user goes idle (idle time is defined in app.js IdleProvider config)
closeModals();
$scope.warning = $modal.open({
templateUrl: 'views/timeoutModal.html',
windowClass: 'modal-danger'
});
});
$scope.$on('IdleTimeout', function () { // This is what happens when the user waits passed the warning timer
logout();
closeModals();
Idle.unwatch();
$scope.$apply();
alert("You have been signed out due to inactivity.");
});
$scope.$on('IdleEnd', function () { // What happens when the user comes back from being idle but before they are timed out
closeModals();
$scope.amIdle = false;
});
基本上,我希望能够简单地告诉我的控制器我想使用这些配置设置,它无需将它们放入控制器就可以使用它们。
你不应该在多个地方需要这个配置。查看 NgIdle,他们正在从 $rootScope 广播事件。 https://github.com/HackedByChinese/ng-idle/blob/develop/angular-idle.js#L193
您需要在不同的范围内设置 watch 的唯一原因是您想要为特定事件执行多项操作。
您需要做的就是在单个控制器中注入 $rootScope 并将事件观察器添加到其中。