Angularjs 单击关闭角度web通知
我正在使用angular web notification(),并且我已经建立了一个工厂,以避免每次需要显示时都进行复制粘贴。我的工厂就在这里Angularjs 单击关闭角度web通知,angularjs,web-notifications,Angularjs,Web Notifications,我正在使用angular web notification(),并且我已经建立了一个工厂,以避免每次需要显示时都进行复制粘贴。我的工厂就在这里 module.registerFactory('browserNotification', function($rootScope, webNotification) { return { show: function(title, body, callback) { webNotification.sho
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()不存在。那么,如何从回调函数中关闭通知呢?这就是诀窍
module.registerFactory('browserNotification', function($timeout,webNotification) {
return {
show: function(title, body, callback) {
var snd = new Audio('audio/alert.mp3');
snd.play();
//the timeout is to sync the sound with the notification rendering on screen
$timeout(function() {
var hideNotification;
webNotification.showNotification(title, {
body: body,
icon: 'img/icon.png',
onClick: function onNotificationClicked() {
callback();
if (hideNotification) {
hideNotification();
}
},
autoClose: 5000 //auto close the notification after 4 seconds (you can manually close it via hide function)
}, function onShow(error, hide) {
if (!error) {
hideNotification = hide;
}
});
}, 150);
}
}
});