简体   繁体   中英

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. That is i want to toggle class between md-primary and 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:

<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

here is working Plunkr hope it make it more clear for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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