我正在使用 angular-web-notification ( https://github.com/sagiegurari/angular-web-notification ),并且我建立了一个工厂,以避免每次我想显示它时都复制粘贴。我的工厂是这个
module.registerFactory('browserNotification', function($rootScope, webNotification) {
return {
show: function(title, body, callback) {
webNotification.showNotification(title, {
body: body,
icon: 'img/icon.png',
onClick: callback,
autoClose: 5000 //auto close the notification after 4 seconds (you can manually close it via hide function)
}, function onShow(error, hide) {
if (error) {
console.log('Unable to show notification: ' + error.message);
} else {
console.log('Notification Shown.');
setTimeout(function hideNotification() {
console.log('Hiding notification....');
hide(); //manually close the notification (you can skip this if you use the autoClose option)
}, 5000);
}
});
}
}
})
如您所见,我向 show() 传递了 3 个变量,其中一个是 onClick 函数的回调,以便在单击通知时执行某些操作。问题是我想在单击该通知后关闭它,但我不知道如何,因为在执行回调函数的上下文中不存在 hide() 函数。例如,在我的控制器中,我有这个
browserNotification.show('Test title', 'Test body', function() {
hide();
alert('Entro al callback!');
});
在那里, hide() 不存在。那么,如何从我的回调函数中关闭通知?