Ionic - How to alternate active mode between 2 button?


#1

Currently in my project, I am able to change colour when my button is active. However, I have 2 buttons. Does anyone know how to change the active mode of these 2 button alternately? When 1 is active, another will be inactive immediately, or vice versa.

html

<ion-view title="Single Projector Room" id="page9">

<ion-nav-buttons side="left">
    <button class="button back-button buttons button-clear header-item" ng-click="goBack()">
      <i class="icon ion-ios-arrow-back"> Back</i> 
    </button>
  </ion-nav-buttons>

  <ion-content padding="true" style="background: url(img/EDD79InSMCeYeMOSchjm_remote.jpg) no-repeat center;background-size:cover;" class="has-header">
    
  <div class="row">
    <div class="col"><br><br><br><br></div>
  </div>

  <div class="row">
  <div class="col col-offset-5 col-30 text-center"><h4 style="color: #FFFFFF;">Single Projector Control</h4></div>
  <div class="col col-30 text-center"><h4 style="color: #FFFFFF;">Screen Control</h4></div>
  </div>

  <div class="row">
    <div class="col col-offset-5 col-30 text-center">
      <button class="button button-large icon-right ion-power" ng-class="{'active': buttonActive}" ng-click="toggleButton()">On</button>
    </div>
    <div class="col col-30 text-center">
      <button class="button button-dark button-large icon-right ion-arrow-up-c">Up</button>
    </div>
  </div>

  <div class="row">
    <div class="col col-offset-5 col-30 text-center">
      <button class="button button-large icon-right ion-android-cancel" "button button-large icon-right ion-power" ng-class="{'active': buttonActive}" ng-click="toggleButton()">Off</button>
    </div>
    <div class="col col-30 text-center">
      <button class="button button-dark button-large icon-right ion-arrow-down-c">Down</button>
    </div>
  </div>

  <div class="row">
    <div class="col"><br><br></div>
  </div>

  <div class="row">
  <div class="col col-offset-5 col-30 text-center"><h4 style="color: #FFFFFF;">Mic Control</h4></div>
  </div>

  <div class="row">
    <div class="col col-offset-5 col-30 text-center">
      <button class="button button-balanced button-large icon-right ion-power">On</button>
    </div>
  </div>

  <div class="row">
    <div class="col col-offset-5 col-30 text-center">
      <button class="button button-assertive button-large icon-right ion-android-cancel">Off</button>
    </div>
  </div>
</div>

  </ion-content>
</ion-view>

controller.js

.controller('singleProjectorRoomCtrl', ['$scope', '$stateParams', function($scope, $stateParams) {

$scope.buttonActive = false;
    
    $scope.toggleButton = function() {
      $scope.buttonActive = !$scope.buttonActive;

    };

$scope.goBack = function() {
window.history.back();
}
}])

#2

use ng-click and ng-class directives. try this…

Buttons - JSFiddle


#3

Try this Snippet to have Toggle Effect

<div class="row">
        <div class="col">
            <button id="reqBtn" class="button button-dark" ng-class="{'active':selectedTab === 'request'}" ng-click="selectedTab = 'request'; toggleData(selectedTab)">Request</button>
        </div>
        <div class="col">
            <button id="respoBtn" class="button button-dark" ng-class="{'active':selectedTab === 'respose'}" ng-click="selectedTab = 'respose'; toggleData(selectedTab)">Response</button>
        </div>
      </div>

and also CSS

.button.button-dark.active{
	background-color: white;
	color: #333;
}