Stack Exchange
how to disable checkbox based on particular value in loop using angular 6?

I want to disable checkbox if value = 2. here is what i tried so far.

discheckcondition = [1,2,3];

for (let x = 0; x < this.discheckcondition.length; x++) {
    // console.log(this.discheckcondition[x]);
    if (this.discheckcondition[x] = '2') {
        console.log(this.discheckcondition[x]);
        this.disablecheckfornext = '1';
    };
}
<ng-container *ngFor="let value of values;">
 <label>
  <input id="checkbox_group1" type="checkbox" pattern="[0-9]{10}" [disabled]=" disablecheckfornext == '1'"  value="value.id" (change)="onCheckboxChange($event)"/>
 </label>
</ng-container>

Can someone help me for the same? here the problem is all text-boxes are getting disabled.

You're using an assignment operator here: if (this.discheckcondition[x] = '2') . Should be == or ===

i tried using it. its not working here.

why you are updating disablecheckfornext  outside ngFor loop. as ng-container is driven by values collection. I suggest values collection should have property like value.disabled, so that can be used as [disabled] = " value.disabled == '1' ".

This code will work if Checkbox is in ngFor loop


discheckcondition = [1,2,3];

for (let x = 0; x < this.discheckcondition.length; x++) {   
        this.disablecheckfornext[x] = false;
    if (this.discheckcondition[x] = '2') {       
        this.disablecheckfornext[x] = true;
    };
}
<input id="checkbox_group1" type="checkbox" pattern="[0-9]{10}" [disabled]=" disablecheckfornext[#i]"  value="" (change)="onCheckboxChange($event)"/>




No Luck for the above solution

You can do it using an index.
<ng-container *ngFor="let value of values;let i = index;">
 <label>
  <input id="checkbox_group1" type="checkbox" pattern="[0-9]{10}" [disabled]=" values[i].id === 2"  value="value.id" (change)="onCheckboxChange($event)"/>
 </label>
</ng-container>


Use attribute '[attr.disabled]' to achieve conditional cases.
<input id="checkbox_group1" type="checkbox" [attr.disabled] = "disablecheckfornext == '1' ? 'disabled' : null" (change)="onCheckboxChange($event)"/>

