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

后端 未结 9 818
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 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.

提交回复
热议问题