2

I want to use single varible for multiple buttons to enable and disable, individually.

<button class="button" [disabled]="btnState" (click)="function('val1')"> button 1</button>
<button class="button" [disabled]="btnState" (click)="function('val2')"> button 2</button>
<button class="button" [disabled]="btnState" (click)="function('val3')"> button 3</button>
<button class="button" [disabled]="btnState" (click)="function('val4')"> button 4</button>

How do I enable or disable the above buttons individually based on selection? I tried only with a result of disabling all of them or enabling all of them.

0

3 Answers 3

2

You can keep track state of the buttons:

<button class="button" [disabled]="buttonStates['button1']" (click)="onToggle('button1')"> button 1</button>
<button class="button" [disabled]="buttonStates['button2']" (click)="onToggle('button2')"> button 2</button>
<button class="button" [disabled]="buttonStates['button3']" (click)="onToggle('button3')"> button 3</button>
<button class="button" [disabled]="buttonStates['button4']" (click)="onToggle('button4')"> button 4</button>


export class YourComponent {
  buttonStates: any = {
    button1: true,
    button2: false,
    button3: false,
    button4: true
  };
  onToggle(button) {
    this.buttonStates[button] = !this.buttonStates[button];
  }
}

Sign up to request clarification or add additional context in comments.

Comments

1

You can use an array, since it's technically a single variable

HTML

<button class="button" [disabled]="btnState[0]" (click)="foo('val1')"> button 1</button>
<button class="button" [disabled]="btnState[1]" (click)="foo('val2')"> button 2</button>
<button class="button" [disabled]="btnState[2]" (click)="foo('val3')"> button 3</button>
<button class="button" [disabled]="btnState[3]" (click)="foo('val4')"> button 4</button>

app.component.ts

btnState = [false, false, true, true];

Stackblitz

Or, you can even use an object

HTML

<button class="button" [disabled]="btnState.btn1" (click)="foo('val1')"> button 1</button>
<button class="button" [disabled]="btnState.btn2" (click)="foo('val2')"> button 2</button>
<button class="button" [disabled]="btnState.btn3" (click)="foo('val3')"> button 3</button>
<button class="button" [disabled]="btnState.btn4" (click)="foo('val4')"> button 4</button>

app.component.ts

btnState = {
    btn1: true,
    btn2: false,
    btn3: true,
    btn4: false,
};

Stackblitz

3 Comments

I strongly prefer the use of an object here. This is not really a good use case for an array, although it would technically work of, course.
Agreed, just putting out the possible ways :)
Thanks.. yep, i was using object using services and DI, though the question i posted has only 4, in my original project i have close 35 buttons as of now and this may grow. hence was looking for a solution with single variable.
0

Why do you want a single variable? If you do, I would use a map for it.

<button class="button" [disabled]="btnState['val1']" (click)="btnClickFunction('val1')"> button 1</button>
<button class="button" [disabled]="btnState['val2']" (click)="btnClickFunction('val2')"> button 2</button>
<button class="button" [disabled]="btnState['val3']" (click)="btnClickFunction('val3')"> button 3</button>
<button class="button" [disabled]="btnState['val4']" (click)="btnClickFunction('val4')"> button 4</button>

and in your clickFunction:

btnClickFunction(val){
   if(!this.btnState.hasOwnProperty(val)){
      this.btnState[val] = false;
   }
   this.btnState[val] = !this.btnState[val];
}

1 Comment

Thanks, will try this one, the orignial project contains more than 35 buttons as of now and it may grow over a period, though using services and object we can achieve with ease, but was looking for more simple and concise version.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.