清除Angularjs中的flash消息超时?
我正在尝试超时以清除我的清除Angularjs中的flash消息超时?,angularjs,Angularjs,我正在尝试超时以清除我的FlashService消息。但它起到了延迟时间的作用 FlashService.Success(("Removed Successfully"), false); 在这里,我使用false作为条件。这意味着当页面或位置更改时,闪存消息被清除 Myflash.service.js function clearFlashMessage() { var flash = $rootScope.flash; if
FlashService
消息。但它起到了延迟时间的作用
FlashService.Success(("Removed Successfully"), false);
在这里,我使用false作为条件。这意味着当页面或位置更改时,闪存消息被清除
Myflash.service.js
function clearFlashMessage() {
var flash = $rootScope.flash;
if (flash) {
if (!flash.keepAfterLocationChange) {
delete $rootScope.flash;
} else {
// only keep for a single location change
flash.keepAfterLocationChange = false;
}
}
}
}
function Success(message, keepAfterLocationChange) {
$rootScope.flash = {
message: message,
type: 'success',
keepAfterLocationChange: keepAfterLocationChange
};
}
function Error(message, keepAfterLocationChange) {
$rootScope.flash = {
message: message,
type: 'error',
keepAfterLocationChange: keepAfterLocationChange
};
}
在我上面的js中,当页面或位置改变时,我正在清除带有“false”标志的flash消息
我需要在错误的条件下设置超时。也就是说,如果标志为false,则需要在某个时间段内清除闪存消息。您需要调用函数在2秒后清除消息,而不是执行
$timeout(fn,interval)
。
i、 e
您需要调用函数在2秒后清除消息,而不是执行
$timeout(fn,interval)
。
i、 e
延迟后需要删除$rootScope.flash。最简单的方法是启动在函数Error和Success中调用的另一个函数。不要忘记注入$timeout:) (功能(){ "严格使用",
angular
.module('app')
.factory('FlashService', FlashService);
FlashService.$inject = ['$rootScope', '$timeout'];
function FlashService($rootScope, $timeout) {
var service = {};
service.Success = Success;
service.Error = Error;
initService();
return service;
function initService() {
$rootScope.$on('$locationChangeStart', function () {
clearFlashMessage();
});
function clearFlashMessage() {
var flash = $rootScope.flash;
if (flash) {
if (!flash.keepAfterLocationChange) {
delete $rootScope.flash;
} else {
// only keep for a single location change
flash.keepAfterLocationChange = false;
}
}
}
}
function clearFlashMessageT() {
console.log("clear after 2 sec started")
$timeout(function(){
delete $rootScope.flash;
}, 2000);
}
function Success(message, keepAfterLocationChange) {
$rootScope.flash = {
message: message,
type: 'success',
keepAfterLocationChange: keepAfterLocationChange
};
clearFlashMessageT()
}
function Error(message, keepAfterLocationChange) {
$rootScope.flash = {
message: message,
type: 'error',
keepAfterLocationChange: keepAfterLocationChange
};
clearFlashMessageT()
}
}
})() 延迟后需要删除$rootScope.flash。最简单的方法是启动在函数Error和Success中调用的另一个函数。不要忘记注入$timeout:) (功能(){ "严格使用",
angular
.module('app')
.factory('FlashService', FlashService);
FlashService.$inject = ['$rootScope', '$timeout'];
function FlashService($rootScope, $timeout) {
var service = {};
service.Success = Success;
service.Error = Error;
initService();
return service;
function initService() {
$rootScope.$on('$locationChangeStart', function () {
clearFlashMessage();
});
function clearFlashMessage() {
var flash = $rootScope.flash;
if (flash) {
if (!flash.keepAfterLocationChange) {
delete $rootScope.flash;
} else {
// only keep for a single location change
flash.keepAfterLocationChange = false;
}
}
}
}
function clearFlashMessageT() {
console.log("clear after 2 sec started")
$timeout(function(){
delete $rootScope.flash;
}, 2000);
}
function Success(message, keepAfterLocationChange) {
$rootScope.flash = {
message: message,
type: 'success',
keepAfterLocationChange: keepAfterLocationChange
};
clearFlashMessageT()
}
function Error(message, keepAfterLocationChange) {
$rootScope.flash = {
message: message,
type: 'error',
keepAfterLocationChange: keepAfterLocationChange
};
clearFlashMessageT()
}
}
})() 这只是延迟显示消息,您需要在服务中进行更改,我猜它在2000毫秒后显示,因为您已经给出了2000毫秒的延迟。这不是你想要的吗?这就是$timeout的工作方式。它在给定延迟后解析函数。我猜你必须在2000毫秒后退出FlashService。然后你必须做一些改变,这只是延迟显示消息,你需要对2000毫秒后显示的服务进行更改,因为你已经给出了2000毫秒的延迟。这不是你想要的吗?这就是$timeout的工作方式。它在给定延迟后解析函数。我猜你必须在2000毫秒后退出FlashService。然后你必须做一些变通,它只清除成功消息。但是div是可见的。如上所述,在我编辑的代码中。@kalai:您需要显示
div
html以获得相应的答案。。。另外,显示FlashService
如何显示消息也会有所帮助。@kalai:请查看我上面更新的代码。我使用一个单独的flash.service.js,并在需要时调用相同的函数。它只清除成功消息。但是div是可见的。如上所述,在我编辑的代码中。@kalai:您需要显示div
html以获得相应的答案。。。另外,显示FlashService
如何显示消息也会有所帮助。@kalai:请查看我上面更新的代码。我使用单独的flash.service.js,并在需要时调用相同的函数。