我需要监听表单是否有任何变化,如果有变化则制作 enable/disable SAVE 按钮 | Angular
I need to listen if there any changes in the form, make enable/disable SAVE button if there changes or not | Angular
在收听页面上的任何更改时遇到问题
有一些信息,即使没有更改 SAVE 按钮激活点击
ngOnInit(): void {
this.createConfigForm()
this.configFilterForm.valueChanges.pipe(takeUntil(this.unsubscribeAll$)).subscribe(value => {
value
})
}
<!-- ADD BUTTON -->
<button
*ngIf="isUpdate()"
[disabled]="configFilterForm"
mat-raised-button
class="add-config-button"
(click)="onSave()"
trackClickSnowplow
id="config-form_save_button"
>
<span>SAVE</span>
</button>
我想我需要订阅,但没有找到可以订阅的地方
在您的 HTML 文件中;
<form [formGroup]="formAngular" (ngSubmit)="onSubmit()">
<label>Name</label>
<input type="text" formControlName="name">
{{formAngular.controls.name.errors | json}}
<p
*ngIf="formAngular.controls.name.errors?.required && formAngular.controls.name.touched && formSend">
It is required your name
</p>
<input type="submit" value="Send" />
<p *ngIf="formSend && !formAngular.valid">Form is not complete</p>
</form>
在您的组件 TS 文件中;
formAngular: FormGroup;
formSend: boolean;
constructor() {
this.formSend = false;
this.formAngular = new FormGroup({
name: new FormControl('', [
Validators.required,
Validators.maxLength(10)
])
}
onSubmit() {
this.formSend = true;
console.log("Form value: ", this.formAngular.value);
}
ngOnInit() {
const controlName = this.formAngular.controls.name;
controlName.valueChanges.pipe(debounceTime(500)).subscribe(value => {
console.log("Your changes: ", value);
});
}
像现在一样收听表单更改,每次有更改时,启用按钮。单击按钮时禁用按钮,它将保持禁用状态,直到发生更改。类似于:
configFilterForm: FormGroup;
isDisabled = true;
constructor(private fb: FormBuilder) {
this.configFilterForm = this.fb.group({
input1: ['hello']
});
this.configFilterForm.valueChanges.pipe(
takeUntil(this.unsubscribeAll$)
).subscribe(value => {
this.isDisabled = false;
});
}
和按钮:
<button [disabled]="isDisabled">Submit</button>
顺便说一句,我看到你在按钮上调用了一些函数 *ngIf="isUpdate()"
。请不要那样做,它们会在每次更改检测时被调用,并且确实会减慢您的应用程序。请改用变量!
在收听页面上的任何更改时遇到问题 有一些信息,即使没有更改 SAVE 按钮激活点击
ngOnInit(): void {
this.createConfigForm()
this.configFilterForm.valueChanges.pipe(takeUntil(this.unsubscribeAll$)).subscribe(value => {
value
})
}
<!-- ADD BUTTON -->
<button
*ngIf="isUpdate()"
[disabled]="configFilterForm"
mat-raised-button
class="add-config-button"
(click)="onSave()"
trackClickSnowplow
id="config-form_save_button"
>
<span>SAVE</span>
</button>
我想我需要订阅,但没有找到可以订阅的地方
在您的 HTML 文件中;
<form [formGroup]="formAngular" (ngSubmit)="onSubmit()">
<label>Name</label>
<input type="text" formControlName="name">
{{formAngular.controls.name.errors | json}}
<p
*ngIf="formAngular.controls.name.errors?.required && formAngular.controls.name.touched && formSend">
It is required your name
</p>
<input type="submit" value="Send" />
<p *ngIf="formSend && !formAngular.valid">Form is not complete</p>
</form>
在您的组件 TS 文件中;
formAngular: FormGroup;
formSend: boolean;
constructor() {
this.formSend = false;
this.formAngular = new FormGroup({
name: new FormControl('', [
Validators.required,
Validators.maxLength(10)
])
}
onSubmit() {
this.formSend = true;
console.log("Form value: ", this.formAngular.value);
}
ngOnInit() {
const controlName = this.formAngular.controls.name;
controlName.valueChanges.pipe(debounceTime(500)).subscribe(value => {
console.log("Your changes: ", value);
});
}
像现在一样收听表单更改,每次有更改时,启用按钮。单击按钮时禁用按钮,它将保持禁用状态,直到发生更改。类似于:
configFilterForm: FormGroup;
isDisabled = true;
constructor(private fb: FormBuilder) {
this.configFilterForm = this.fb.group({
input1: ['hello']
});
this.configFilterForm.valueChanges.pipe(
takeUntil(this.unsubscribeAll$)
).subscribe(value => {
this.isDisabled = false;
});
}
和按钮:
<button [disabled]="isDisabled">Submit</button>
顺便说一句,我看到你在按钮上调用了一些函数 *ngIf="isUpdate()"
。请不要那样做,它们会在每次更改检测时被调用,并且确实会减慢您的应用程序。请改用变量!