首页 文章

不使用验证表单如果<input>字段为空,如何禁用角度按钮

提问于
浏览
-1

我正在尝试以角度为 @output ,因为我有一个带按钮的输入字段,我将使用@output()将输入的字段数据发送到父级我想要在输入字段为空时禁用按钮,如何禁用不使用表单验证 .

我试过这段代码

<input type="text" [(ngModel)] = "childInput" >
  <button [disabled] ="!childInput.value" (click)="sendToParent()">Send To Parent</button>

但如果输入字段为空,它会一直禁用该按钮 .

P.S我找到了表单验证的答案,但我不想做表单验证或定义bool变量

2 回答

  • 3

    这段代码可行

    <input type="text" [(ngModel)] = "childInput" >
    <button [disabled] ="!childInput" (click)="sendToParent()">Send To Parent</button>
    
  • 0

    如果您不想使用ngModel:

    <input type="text" #childInput >
    <button [disabled]="!childInput.value.length>0" (click)="sendToParent()">Send To Parent</button>
    

相关问题