简体   繁体   English

如果Angular 6中的textarea为空,如何禁用复选框

[英]How to disable checkbox if textarea empty in Angular 6

I have textarea and checkbox in my Angular template. 我的Angular模板中有textarea和复选框。 Textarea can be with or without value, it depends of my api. Textarea可以带有或不带有值,这取决于我的API。 I want to add attribute disabled to my checkbox if textarea empty, and does not add if textarea has it 如果textarea为空,我想将禁用属性添加到我的复选框,如果textarea有,则不添加属性

Something like: 就像是:

<textarea name="descriptionText" formControlName="descriptionText">
</textarea>
<input type="checkbox" [disabled]="!descriptionText.length" />

You can use ngModel to achieve this goal 您可以使用ngModel达到此目标

<textarea [(ngModel)]="text"></textarea>
<input type="checkbox" [disabled]="!text || text.length <= 0" />

Adding !text check you can avoid to declare text var into your ts component. 添加!text check可以避免在ts组件中声明text var。

Remember to import ngModel into your ts component like this: 记住要像这样将ngModel导入ts组件:

import { NgModule } from '@angular/core';

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

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