Search code examples
angularjsflash-message

Flash Messages don't disappear (sachinchoolur flash module) AngularJs


I am trying to make flash messages in my AngularJs app with the use of sachinchoolur's angularjs-flash module.

The flashes work but they don't disappear after the set TimeOut.

I followed the documentation correctly and I made a minimum code in plnkr to demonstrate the problem.

https://plnkr.co/edit/OaLbAjqZDmeWoPxr5uaf?p=preview

app.js

// public/js/app.js
angular.module('myApp', ['ngFlash'


])
.config(['$locationProvider', '$httpProvider', 'FlashProvider',
  function($locationProvider, $httpProvider, FlashProvider) {
    FlashProvider.setTimeout(5000);

}])


.controller('MainCtrl', function($scope, $rootScope, $http, $location, $window, Flash) {

  $scope.test = "test";
  $scope.show = function() {
      var message = 'Welcome '
      var id = Flash.create('success', message, 0, {class: 'custom-class', id: 'custom-id'}, true);
      alert("show");
  }
});

html template:

<!DOCTYPE html>
<html ng-app="myApp">

  <head>
    <link data-require="bootstrap@*" data-semver="4.0.5" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0-alpha.6/css/bootstrap.min.css" />

    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.6/angular.min.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/angular-ui-router/0.2.0/angular-ui-router.js"></script>
    <script type="text/javascript" src="//cdnjs.cloudflare.com/ajax/libs/script.js/2.4.0/script.min.js"></script>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>


    <script type="text/javascript" src="app.js"></script>
    <script type="text/javascript" src="angular-flash.js"></script>
  </head>

  <body ng-controller="MainCtrl">

    <h1>Hello Plunker!</h1>
    {{ test }}

    <flash-message duration="5000"></flash-message>
    <button ng-click="show()">Test</button>


  </body>

</html>

Solution

  • According to how to use | angular-flash for Flash.create(...),

    Third argument (number, optional) is the duration of showing the flash. 0 to not automatically hide flash (user needs to click the cross on top-right corner).

    Hence, I changed your Flash.create function to contain 5000 instead of 0 which was your timeout:

    Flash.create('success', message, 5000, {
      class: 'custom-class',
      id: 'custom-id'
    }, true);
    

    And, it works!

    Here's working example