简体   繁体   English

单击事件时切换md-button类

[英]Toggle md-button class on click event

I want to use button as a selector, i have a toggle function on click event to get the value from the button click, now what i want to do is to show the user that the button is selected. 我想使用按钮作为选择器,我在click事件上具有切换功能,以从按钮单击中获取值,现在我要做的是向用户显示按钮已被选择。 That is i want to toggle class between md-primary and md-warn. 那就是我想在md-primary和md-warn之间切换类。

This is my button 这是我的按钮

<md-button  class="md-fab" ng-click="getTime(0);" 
  value="0" aria-label="midnight" ng-class="{'active': 
  variable,'disable': !variable}">0</md-button>

This is the controller part 这是控制器部分

 $scope.getTime = function (value) {
        $scope.variable = !$scope.variable;
        if($scope.variable){
            console.log(value);
        }
    };

update ng-class like below: 更新ng-class如下:

<md-button  class="md-fab" ng-click="getTime(0);" 
  value="0" aria-label="midnight" ng-class="{'md-primary': 
  variable,'md-warn': !variable}">0</md-button>

also make sure to add ngMaterial to your app module dependencies 还要确保将ngMaterial添加到您的应用模块依赖项中

here is working Plunkr hope it make it more clear for you. Plunkr希望在这里为您提供更多帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM