1

Just Starting out on AngularJS. I would like to create a self close message. How do we do this?

Basically I am attempting to produce the results similar to the following question:

How to Automatically Close Alerts using Twitter Bootstrap

But with AngularJS only.

I'm using the following code through an array. That doesn't work.

 $timeout(function () {
        stack.push(messages.pop());
        checkStack();
    },5000);
4
  • I'm looking at this for now but it seems like too much. Perhaps there is an easier (i.e. purer way!) github.com/DerekRies/Angular-Notifications Commented Feb 13, 2014 at 16:31
  • 1
    Are you creating your own message box? If so, create a directive for your message box and in the link function, use the $timeout service to close it. Commented Feb 13, 2014 at 16:38
  • a simple directive with docs.angularjs.org/api/ng.$timeout when the time is over remove alert from the dom Commented Feb 13, 2014 at 16:43
  • Ended up using the $timeout service. Commented Feb 13, 2014 at 17:08

1 Answer 1

2

You can definitely use a directive to implement similar functionality without using jquery at all. Just use the $timeout function.

app.directive('myAlert', function($timeout){
  return {
    scope: {
      message: '@',
      isVisible: '='
    },
    link: link,
    restrict: 'E',
    replace: true,
    template: '<div ng-if="isVisible" class="alert">{{message}}</div>'
  }

  function link(scope, element, attrs){
    scope.isVisible = true;

    $timeout(function (){
            scope.isVisible = false;
            }, 5000);

  }
});

Take a look at this plunker: http://plnkr.co/edit/2ApTMAHjvMsq8OE1cInB?p=preview

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.