简体   繁体   English

如何使用 angular 6 根据循环中的特定值禁用复选框?

[英]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.如果 value = 2,我想禁用复选框。这是我迄今为止尝试过的。

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如果 Checkbox 在 ngFor 循环中,此代码将起作用

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM