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

2

13

What I want?

Like any other app, when alerts show up, they hide after a second or so, I am trying to find out a way to hide the alert after a second, but not sure how to do that

Any help is greatly appreciated

UPDATE

As per @Derek's answer, I am able to implement timeout
http://plnkr.co/edit/uqSB1gIz6XEmJfC8zHNb?p=preview

Organize answered 29/5, 2013 at 21:28 Comment(3)
I wrote simple notification plugin using notify... may be you can check it out code-like-a-poem.blogspot.in/2013/11/…Iridis
This isn't working for me :( made exactly what you've done in the plunkr and it didn't workArbitress
@Arbitress The plunkr is using a pretty old version of angular, I'm pretty sure the requirements for the directive has changed, mainly the scopeMonopteros
W
29

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: '<div class="alert fade" bs-alert="ngModel"></div>',
     link: function(scope, element, attrs){
         $timeout(function(){
             element.remove();
         }, 5000);
     }
  }
});
Wartow answered 29/5, 2013 at 21:35 Comment(6)
Hi Derek, how would you implement this as an array? just curiousOrganize
I just recently made an angular component for doing notifications you can check out that does this. (might be different from what you're looking for though). Github Link!. Sorry there's no demo yet, but hopefully you can see a little bit of what I'm talking about.Wartow
How is same thing possible when I need to change my messages? I have a plunker here - plnkr.co/edit/YioiJXNkaET6T2mexjCq?p=previewOrganize
btw, I have added question #16826261Organize
Update your plunk to reference at least angluarjs 1.3.8 and the plnkr starts to work again. Which works for the first 'round', but some error stops more clicks popping up alerts.Benjamin
@Organize PLEASE CHECK THE JS HERE angular-ui.github.io/bootstrap/#/alert it has an implementation for an array to push messages .Plowshare
Q
4

I have updated plunker created by @daydreamer to show multiple alerts and hide automatically. If anybody wants to customized multiple alerts have a look at this Plunker Link

Half of the code same as @DerekR, I have only made customization to it

var app = angular.module('myApp', ['$strap.directives']);

app.directive('notification', function($timeout){
  return {
    restrict: 'E',
    replace: true,
    scope: {
      ngModel: '='
    },
    template: '<div class="alert fade" bs-alert="ngModel"></div>',
    link: function(scope, element, attrs) {
      $timeout(function(){
        element.hide();
      }, 3000);
    }
  }
});

app.controller('AlertController', function($scope){
    $scope.message = {
      "type": "info",
      "title": "Success!",
      "content": "alert directive is working pretty well with 3 sec timeout"
    };

    $scope.alerts = [];
    $scope.addAlert = function(index) {
      $scope.alerts.push(
          {
            "type": "info",
            "title": "Success!" + index,
            "content": "alert "  + index + " directive is working pretty well with 3 sec timeout"
          }
      )
    }
});
Quilting answered 17/10, 2015 at 12:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.