我正在尝试暂停以清除FlashService
消息。但它可以作为延迟时间。
FlashService.Success(("Removed Successfully"), false);
在此我使用false作为条件。这意味着当页面或位置发生变化时,闪存消息将被清除。
我的 flash.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消息。
我需要在该错误条件下设置超时。也就是说,如果flag为false,则需要在一段时间内清除flash消息。
答案 0 :(得分:1)
您需要在2秒后调用该函数清除消息 - 而不是执行$timeout(fn, interval)
。
即。
FlashService.Success(("Removed Successfully"), false);
$timeout(function(){
//clear message
//FlashService.ClearMessage(); - or whatever how you clear the message
}, 2000);
答案 1 :(得分:-1)
您需要在延迟后删除$ rootScope.flash。最简单的方法是启动另一个在错误和成功函数中调用的函数。不要忘记注入$ timeout:)
(function(){ “严格使用”;
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()
}
}
})();