Angular Material: Custom Toast Service

Standard

Customize Angular toast service, register within your controller and other data services and use it throughout your project. For html code clone my repo from github.

Github Link

Angular Code:

(function () {
    'use strict';

    var app = angular.module('app', ['ngMaterial']);

    app.controller('toastController', function (toastAlert) {

        var vm = this;
        
        vm.defaultToaster = function () {
            toastAlert.defaultToaster('Toast Activated');
        };

        vm.customToaster = function () {
            toastAlert.customToaster('Toast Activated', '3000', 'bottom left');
        };
    });

})();

Toaster Service:

(function () {
    'use strict';

    angular
        .module('app')
        .factory('toastAlert', toastAlert);

    toastAlert.$inject = ['$mdToast'];

    function toastAlert($mdToast) {

        var service = {
            customToaster: customToaster,
            defaultToaster: defaultToaster
        };

        return service;

        ////////////////////////////Implementation//////////////////////////////////////

        function customToaster(content , delay, position) {
            $mdToast.show(
              $mdToast.simple()
                .textContent(content)
                .position(position)
                .hideDelay(delay)
            );
        }

        function defaultToaster(content) {
            $mdToast.show(
              $mdToast.simple()
                .textContent(content)
                .position('bottom right')
                .hideDelay(3000)
            );
        }
    }
})();

 

Screenshot_1

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s