0

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.

3
  • You're using an assignment operator here: if (this.discheckcondition[x] = '2') . Should be == or === Commented Jan 8, 2020 at 4:47
  • i tried using it. its not working here. Commented Jan 8, 2020 at 5:11
  • 1
    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' ". Commented Jan 8, 2020 at 5:30

3 Answers 3

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)"/>

1
  • No Luck for the above solution Commented Jan 8, 2020 at 5:30
1

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>
0

Use attribute '[attr.disabled]' to achieve conditional cases.

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.