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

后端 未结 9 800
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:55

    You did ask about using the simple toast call. Would you mind trying a custom toast show like the demo and add classes to the template?

    JS

    $mdToast.show(
      controller: 'ToastCtrl',
      templateUrl: 'views/shared/toast.html',
      hideDelay: 6000,
      position: $scope.getToastPosition()
    )
    

    TEMPLATE

    <md-toast class="flash">
      <span flex>Custom toast!</span>
      <md-button ng-click="closeToast()">
       Close
      </md-button>
    </md-toast>
    

    CSS

    md-toast.flash {
      background-color: red;
      color: white;
    }
    

    CONTROLLER (COFFEE)

    'use strict'
    
    ###*
     # @ngdoc function
     # @name angularApp.controller:ToastCtrl
     # @description
     # # ToastCtrl
     # Controller of the angularApp
    ###
    angular.module('angularApp')
      .controller 'ToastCtrl', ($scope) ->
        $scope.closeToast = ()->
          $mdToast.hide()
    
    0 讨论(0)
  • 2021-02-03 18:03

    EDIT:
    For a correct implementation, please use rlay3s solution below :)!

    OUTDATED:
    I had problems displaying custom text with jhblacklocks solution, so I decided to do it like this using 'template':

    var displayToast = function(type, msg) {
    
        $mdToast.show({
            template: '<md-toast class="md-toast ' + type +'">' + msg + '</md-toast>',
            hideDelay: 6000,
            position: 'bottom right'
        });
    };
    

    I also added the different types in my .css file:

    .md-toast.error {
        background-color: red;
    }
    
    .md-toast.success {
        background-color: green;
    }
    

    Don't know if this is the most beautiful approach, but I don't need a template files for each dialog type and displaying custom text is really easy.

    0 讨论(0)
  • 2021-02-03 18:05

    I first favoured the solution but I don't like setting up a theme in the theme provider just for a toast. So how about this solution:

    JS (Coffee)

       if error
          message = ''
    
          if error.reason is 'Incorrect password'
            message = 'Email and password combination is incorrect'
          if error.reason is 'User not found'
            message = 'No account found with this email address'
    
          $mdToast.show(
            templateUrl:  'client/modules/toasts/toastError.html'
            hideDelay:    3000
            controller: ( $scope ) ->
              $scope.message    =  message
              $scope.class      = 'error'
              $scope.buttonLabel = 'close'
              $scope.closeToast = ->
                $mdToast.hide()
          ).then( ( result ) ->
            if result is 'ok'
              console.log('do action')
          )
    

    and then HTML (JADE)

    md-toast(ng-class="class")
      span(flex) {{message}}
      md-button.md-action(ng-click="closeToast()") {{buttonLabel}}
    

    I tried to use the locals option to pass variables to the controller, but for some reason they are not injected.(https://material.angularjs.org/latest/#/api/material.components.toast/service/$mdToast check list of options under show function)

    Then last the CSS (STYLUS)

    md-toast.success
      background-color    green
    
    md-toast.error
      background-color    red
    

    Summary: there is on template in this case which you can give custom placeholders which you prefill in your controller. This solution works for me.

    0 讨论(0)
提交回复
热议问题