how can I change the color of Toast depends on message type in Angular material $mdToast?

后端 未结 9 817
Happy的楠姐
Happy的楠姐 2021-02-03 17:10

While using $mdToast.simple().content(\"some test\") it is showing the toast with black color. how can I change that color to red, yellow and so, depends on the typ

9条回答
  •  爱一瞬间的悲伤
    2021-02-03 17:43

    You can do it with factory and some css.

    (function () {
      'use strict';
    
      angular
        .module('app.core')
        .factory('ToastService', ToastService);
    
      /** @ngInject */
      function ToastService($mdToast) {
    
        var service = {
          error: error,
          success: success,
          info : info
        };
    
        return service;
    
        //////////
    
        function success(text) {
          $mdToast.show(
            $mdToast.simple()
              .toastClass("toast-success")
              .textContent(text)
          );
        }
    
        function info(text) {
          $mdToast.show(
            $mdToast.simple()
              .toastClass("toast-info")
              .textContent(text)
          );
        }
    
        function error(text) {
          $mdToast.show(
            $mdToast.simple()
              .toastClass("toast-error")
              .textContent(text)
          );
        }
      }
    }());
    

    And css.

    .toast-error .md-toast-content{
      background-color: rgb(102,187,106) !important;
    }
    
    .toast-info .md-toast-content{
      background-color: rgb(41,182,246) !important;
    }
    
    .toast-error .md-toast-content{
      background-color: rgb(239,83,80) !important;
    }
    

提交回复
热议问题