2016-10-14 1 views
2

J'utilise angular-web-notification (https://github.com/sagiegurari/angular-web-notification) et j'ai construit une usine afin d'éviter le copier-coller à chaque fois que je veux l'afficher. Mon usine est ceFermer angular-web-notification onclick

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); 
       } 
      }); 
     } 
    } 
}) 

Comme vous pouvez le voir, je passe à l'émission() 3 variables, l'un d'eux est un rappel pour la fonction onClick, afin de faire des choses quand la notification est cliqué. La chose est que je veux fermer cette notification une fois qu'il est cliqué, mais je ne peux pas comprendre comment, car les fonctions hide() n'existent pas dans le contexte où la fonction de rappel est exécutée. Par exemple, dans mon contrller j'ai ceci

browserNotification.show('Test title', 'Test body', function() { 
     hide(); 
     alert('Entro al callback!'); 
    }); 

Là, hide() n'existait pas. Alors, comment puis-je fermer la notification de ma fonction de rappel?

Répondre

0

Cela fait l'affaire!

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); 
      } 
     } 
    });