• Download
  • Contact
  • Terms of Service
  • Privacy Policy
  • About US
Codershood
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others
No Result
View All Result
Codershood
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others
No Result
View All Result
Codershood
No Result
View All Result

Showing notification using toaster in AngularJs

by Shashank Tiwari
August 11, 2018
in AngularJS
3
3 Minutes Read
shwoing notification using toaster for angularjs

Last time I published an article on real-time chat using NodeJS and Socket.io, I didn’t include this module into that application. So in this article am sharing a library to show the notifications in applications like chatting, when a new user joins the Chatroom or any other occasion when you want to show the notification.

This library is in AngularJs as our Chat application was in Angularjs However, you can find the Jquery plugin for the same from toastr in jquery.

Also, read Notification using ng-notify in AngularJs

 Download

 Demo




Now to use this library we need to include three files listed below,

  1. toaster.css
  2. toaster.js
  3. angular-animate.min.js

After including these files, we are good to go.

But before that we have to include these dependencies too in order to use toaster like this,
angular.module('sample', ['ngAnimate', 'toaster']);

To create basic toaster we need to create this DOM Element

<toaster-container toaster-options="{'time-out': 3000,'position-class': 'toast-top-right'}"></toaster-container>

After creating this element, we can show the notification by using below line

toaster.pop('success', "Notificaton", "Hello codershood.info", 5000, 'trustedHtml');

In the below code, you will find the various ways to use this library.

index.html

<!DOCTYPE html>
<html ng-app="sample">
<head>
    <title>angular-toaster-sample!</title>
    <link href="http://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.1/css/bootstrap.min.css" rel="stylesheet" />
    <link href="../libs/angularjs-toaster/toaster.min.css" rel="stylesheet" />
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.3.3/angular.min.js" ></script>
    <script src="http://code.angularjs.org/1.3.3/angular-animate.min.js" ></script>
    <script src="../libs/angularjs-toaster/toaster.min.js"></script>
    <script src="script.js"></script>
    <link href="style.css" rel="stylesheet" />
</head>
<body>

    <div ng-controller="HomeController" class="container" >
       
       <span>Simple Notifications:</span>
        <div class="rows">
            <div class="col-md-2"><button class="btn btn-primary" href ng-click="simple()">Simple notification</button></div>
            <div class="col-md-2"><button class="btn btn-warning" href ng-click="warning()">Warning notification</button></div>
            <div class="col-md-2"><button class="btn btn-success" href ng-click="success()">Success notification</button></div>
            <div class="col-md-2"><button class="btn btn-danger" href ng-click="error()">Error notification</button></div>
            <div class="col-md-2"><button class="btn btn-grimace" href ng-click="wait()">Wait notification</button></div>
            <div class="col-md-2"><button class="btn btn-primary" href ng-click="pop()">Link to other page</button></div>
        </div>
        <hr/> 
        <span>Addding Option from Script:</span>
        <div class="rows">
            <div class="col-md-6">
                <Span>With CLose button</span>
                <button class="btn btn-primary" href ng-click="close()">Close Button </button>
            </div>
            <div class="col-md-6">
                <Span>Fade after 1 Sec</span>
                <button class="btn btn-primary" href ng-click="timeout()">Html notification</button>
            </div>
        </div>
        <br/>
        <hr/>
        <span>Custom Notification and Body output type:</span>
        <div class="rows">
            <div class="col-md-3"><button class="btn btn-primary" href ng-click="Custom_temp()">Custom template</button></div>
            <div class="col-md-3"><button class="btn btn-primary" href ng-click="trusted_html()">Trusted HTML</button></div>
            <div class="col-md-3"><button class="btn btn-primary" href ng-click="default_temp()">Default Template</button></div>
            <div class="col-md-3"><button class="btn btn-primary" href ng-click="file_custom_temp()">Including file from Folder</button></div>
        </div>
        
        <!-- TO show the Notification Basic -->
        <toaster-container toaster-options="{'time-out': 3000,'position-class': 'toast-top-right'}"></toaster-container>


        <!-- If you Get Null values in your Function by defalut show this Template --> 
        <script type="text/ng-template" id="toasterBodyTmpl.html">
          <p>Null values ! Here is your default template</p>
        </script>
        
        <!-- Including Custom template -->
        <script type="text/ng-template" id="myTemplate.html">
            <div class="Custom_temp_html">
                <p>Hello this is my Customm Template</p>
            </div>
        </script>        

        <!-- Including Template as well as data from Script -->
        <script type="text/ng-template" id="myTemplateWithData.html">
          <p>Here it is! {{toaster.data}}</p>
        </script>

        <!-- Incuding File From another folder -->
        <script toaster-options="{'time-out': 3000,'position-class': 'toast-top-center','body-output-type': 'template'}" id="myTemplate" >
          <p>it is! </p>
        </script>

    </div>

</body>

 

If you see the below code line number 49 used to show the basic toaster as we discussed above. We can also use Custom HTML file which can be from the different folder, see line number 59 and 70.

Now to power our HTML file let’s create script.js to show the notifications.

Script.js

ngular.module('sample', ['ngAnimate', 'toaster'])
.directive('bindUnsafeHtml', [function () {
            return {
                template: "<span style='color:orange'>Orange directive text!</span>"
            };
        }])
// The directive that will be dynamically rendered
.directive('bindName', [function () {
      return {
          template: "<span style='color:orange'>Hi {{directiveData.name}}!</span>"
      };
}])
.controller('HomeController', function($scope, toaster, $window,$sce) {
    
    $scope.pop = function(){
       //toaster.toast.body='toast-top-center'
       toaster.pop('success', "title", 'Its address is https://google.com.', 5000, 'trustedHtml', function(toaster) {
            var match = toaster.body.match(/http[s]?:\/\/[^\s]+/);
            if (match) $window.open(match[0]);
            return true;
        }); 
               

    };    
    $scope.simple = function(){
        toaster.pop('note', "title", "text");
    };
    $scope.warning = function(){
        toaster.pop('warning', "warning",'Hello codershood.info');
    };
    $scope.success = function(){
        //toaster.success({title: "title", body:"text1"});
        toaster.pop('success', "Notificaton", "Hello codershood.info", 5000, 'trustedHtml');
    };
    $scope.error = function(){
       toaster.error("Error", "You broke something");
    };
    $scope.wait = function(){
        //toaster.pop({type: 'wait', title: "title", body:"text"});
        toaster.wait("Wating","Wating");
    }; 
    $scope.Custom_temp = function(){
        toaster.pop('warning', "This is my Template", "myTemplate.html", null, 'template');
    };
    $scope.close = function(){
        toaster.pop({
            type: 'error',
            title: 'Title text',
            body: 'Body text',
            showCloseButton: true,
            closeHtml: '<button>Close</button>'
        });
    };
    $scope.timeout=function(){
        toaster.pop('note', "TimeOut", "text");
        toaster.toast.timeout=1000;
    };
    $scope.trusted_html=function(){
        toaster.pop('error', "title", '<p class="trustedHtml_text">You directly include HTMl from Script.</p>', null, 'trustedHtml');
    };
    $scope.default_temp=function(){
        toaster.pop('note', "title", null, null, 'template');
    };
    $scope.file_custom_temp=function(){
       toaster.pop('warning', "This is my Template", "templates/Template.html", null, 'template');
    };

    $scope.position=function(){
        toaster.pop('note', "title", 'toast-bottom-left', 'toast-bottom-left', 'tost_id');
        console.log(toaster);
    }
    
    $scope.goToLink = function(toaster) {
      var match = toaster.body.match(/http[s]?:\/\/[^\s]+/);
      if (match) $window.open(match[0]);
      return true;
    };
    
    $scope.clear = function(){
        toaster.clear();
    };
});

 

For now, that’s it When I find libraries like this I will always with you guys. You can use this library in your project to enhance the web experience.

Further reading and links:

GitHub page of AngularJs toastr.

GitHub page of Jquery toastr.

If you find this article helpful consider sharing this with other hoodies.

Tags: Angular ToasterNotificationNotificationsPlugins and LibrariesToaster
Previous Post

Showing online users using Nodejs and Socket.io

Next Post

Integrating Ladda buttons in AngularJs

Related Posts

ng-notify notifications using angularjs
AngularJS

Notification using ng-notify in AngularJs

August 11, 2018
Integrating Ladda buttons in AngularJs
AngularJS

Integrating Ladda buttons in AngularJs

August 11, 2018
Showing online users using Nodejs and Socket.io
AngularJS

Showing online users using Nodejs and Socket.io

August 8, 2018
Next Post
Integrating Ladda buttons in AngularJs

Integrating Ladda buttons in AngularJs

Comments 3

  1. Alex H says:
    9 years ago

    If you get an “Unexpected token <" error on the last script tag of your index.html file, you will likely need to add a "type = 'text/ng-template' " attribute to that script tag.

    Reply
    • Shashank says:
      9 years ago

      Thank you Alex, it will other users too.

      Reply
  2. Home Plix says:
    6 years ago

    Hi

    So amazing post here we found, I like it and appreciated your works.
    Thanx for your kind info.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *




https://codershood.info

www.codershood.info programming blog dedicated to providing high-quality coding tutorial and articles on web development, Angular, React, Laravel, AngularJs, CSS, Node.js, ExpressJs and many more. We also provide ebook based on complicated web application along with the source code.

  • Download
  • Contact
  • Terms of Service
  • Privacy Policy
  • About US

www.codershood.info is licensed under a Creative Commons Attribution 4.0 International License.

No Result
View All Result
  • Demos
  • Plugins
  • Angular
  • NodeJs
  • GO lang
  • Others

www.codershood.info is licensed under a Creative Commons Attribution 4.0 International License.