gpt4 book ai didi

angular - 根据 Angular 值的变化禁用/启用按钮

转载 作者:行者123 更新时间:2023-12-03 08:08:03 25 4
gpt4 key购买 nike

我有一个包含不同输入字段的表单,包括 input[type="file"] 和 input[type="checkbox"]。我通过后端的 get 请求获取输入表单字段的值,并将它们存储在公司变量中。

ngOnInit(): void {
this.generateForm();

this.companyService.getCompany().subscribe((response: Company) => {
this.company = response;

this.settingsForm.setValue({
...this.company,
certificateUrl: [this.company.certificateUrl],
});
});
}

我首先生成表单,然后从 get 请求获取响应,这样我就可以将我的公司变量设置为响应,并根据我得到的值设置表单的值。问题是我想在表单有效和更改时启用 html 中的按钮。因此,当未根据先前值进行任何更改时,应禁用按钮。例如:如果我们的字段名称值为“myName”,并且我在输入中键入“my”按钮,则应启用该按钮,因为与之前的值相比发生了更改。如果我输入回“myName”,它应该被禁用,因为值与之前的值相同。如果字段为空并且用户输入某些内容并且对于字段类型文件和复选框,也会发生同样的情况。

这就是我目前正在尝试的方法。

inputChanged: boolean = false

ngOnInit() {
this.settingsForm.valueChanges.subscribe((currentValue) => {
if (JSON.stringify(currentValue) !== JSON.stringify(this.company)) {
this.inputChanged = true;
} else {
this.inputChanged = false;
}
});
}
////

<button
[disabled]="!inputChanged || !settingsForm.valid"
class="btn btn-md text-light"
>
Save
</button>

这是我的表格

<form class="mt-4" [formGroup]="settingsForm" (ngSubmit)="saveChanges()">
<div class="row">
<div class="col-6">
<div class="form-group">
<label class="form-label fw-bold">ID</label>
<input
formControlName="identificationNumber"
type="text"
class="form-control"
/>
</div>
<div class="form-group d-none">
<label class="form-label fw-bold">Id</label>
<input formControlName="id" type="text" class="form-control" />
</div>
<div class="form-group d-none">
<label class="form-label fw-bold">Country</label>
<input formControlName="country" type="text" class="form-control" />
</div>

<div class="form-group">
<label class="form-label fw-bold"
>P</label
>
<input formControlName="tin" type="text" class="form-control" />

</div>
<div class="form-group">
<label class="form-label fw-bold">Name</label>
<input formControlName="name" type="text" class="form-control" />
</div>
</div>
<div class="col-6">
<div class="form-group">
<label class="form-label fw-bold">City</label>
<input formControlName="city" type="text" class="form-control" />
</div>

<div class="form-group">
<label class="form-label fw-bold">Address</label>
<input formControlName="address" type="text" class="form-control" />
</div>
<div class="form-group">
<label class="form-label fw-bold">Postal code</label>
<input
formControlName="postalCode"
type="text"
class="form-control"
/>
</div>
<div class="form-check ms-1">
<label class="form-check-label fw-bold">Vat</label>
<input
formControlName="inVat"
type="checkbox"
class="form-check-input"
[checked]="company?.inVat"
/>
</div>
</div>
</div>

<div class="row align-items-end justify-content-between">
<div class="col-8">
<app-file-upload
[company]="company"
formControlName="certificateUrl"
></app-file-upload>
</div>

<div class="col-4">
<button
[disabled]="!inputChanged || !settingsForm.valid"
class="btn btn-md text-light"
>
Save changes
</button>
</div>
</div>
</form>

但是这不起作用。我该如何解决这个问题?

最佳答案

您可以使用other响应式(Reactive)表单属性

使用pristine为例

A control is pristine if the user has not yet changed the value in the UI.

<button
[disabled]="settingsForm.pristine || settingsForm.invalid"
class="btn btn-md text-light"
>Save</button>

如果您需要更具体的控制值,它会变得更加棘手,如 comparing Javascript objects并不是那么微不足道。

这个question是一次很棒的深入研究

关于angular - 根据 Angular 值的变化禁用/启用按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/71585757/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com