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

后端 未结 2 458
说谎
说谎 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:38

    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: '
    ', 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" } ) } });

提交回复
热议问题