简体   繁体   中英

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.

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

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>

使用属性'[attr.disabled]'来实现条件情况。

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

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