如何使用formControl禁用单个按钮?

问题描述 投票:0回答:1

如果没有选择复选框,我必须禁用这个按钮。

<button (click) = "onDeleteSelected()" [disabled] = "mflagDisableMoveTo.value"> Delete selected </button>

在typecript中,我有。mflagDisableMoveTo = new FormControl( { value: 'true' } )

这个表单控件不是任何formGroup的一部分。

我应该把 [formControl] = "mflagDisableMoveTo" 在上述HTML代码中?

如何使用 formControl 禁用一个单独的按钮?

angular typescript angular-reactive-forms
1个回答
2
投票

起初,true不是一个字符串,所以你需要写true,而不是'true'。

mflagDisableMoveTo = new FormControl({ value: true })

你可以通过以下方式获得任何FormControl的值 FormControl.value 由于你有new FormControl({ value: true }),正确的答案是:mflagDisableMoveTo.value.value。

<button (click)="onDeleteSelected()" [disabled] = "mflagDisableMoveTo.value.value"> Delete selected </button>

1
投票

您好:formControl的初始值必须是假的,我的做法是在禁用按钮中使用formControl的值。

1.在app.component.ts中创建我的formControl。

import { FormControl, Validators } from "@angular/forms";

 check = new FormControl(false, [Validators.required]);

2.在app.component.html中创建我的formControl <input type="checkbox" [formControl]="check" /> <button (click)="showControl()" [disabled]="check.value === false">Send</button>

© www.soinside.com 2019 - 2024. All rights reserved.