[英]how to disable checkbox based on particular value in loop using angular 6?
如果 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>
有人可以幫助我嗎? 這里的問題是所有文本框都被禁用了。
如果 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)"/>
您可以使用索引來完成。
<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.