AngularJS Directive: How do I hide the alert using timeout?

后端 未结 2 477
说谎
说谎 2021-02-01 08:42
  • Yesterday, I started writing a notification directive for my project
  • I asked question on stackoverflow AngularJS: Alerts not showing up and after s
2条回答
  •  野性不改
    2021-02-01 09:39

    Generally I would implement notifications with an array, that pushes new notifications onto the stack, then sets a $timeout that removes that particular element from the array. On the rendering side you would just use an ng-repeater.

    However in your case, if you want to keep your existing directive you could accomplish this functionality by just adding a linking function and setting a $timeout to remove the element.

    app.directive('notification', function($timeout){
      return {
         restrict: 'E',
         replace: true,
         scope: {
             ngModel: '='
         },
         template: '
    ', link: function(scope, element, attrs){ $timeout(function(){ element.remove(); }, 5000); } } });

提交回复
热议问题