gpt4 book ai didi

angular - 如何使用 Angular 重置自定义表单控件

转载 作者:太空狗 更新时间:2023-10-29 17:31:57 25 4
gpt4 key购买 nike

我有一个带有验证的自定义表单控件。我在其中使用独立的 FormControl 来处理值和一些验证。

当控件从另一个 FormGroup 重置时,Angular 是否有办法重置内部 FormControl?

Bellow 是我的自定义表单控件。我希望能够重置 durationControl。

duration.component.ts

import { ChangeDetectionStrategy, Component, Input, OnInit, Optional, Self } from '@angular/core';
import { AbstractControl, ControlValueAccessor, FormControl, NgControl, ValidationErrors, Validators } from '@angular/forms';
import { Observable } from 'rxjs';
import { map, startWith, tap } from 'rxjs/operators';
import { Regex } from '../../constants';

@Component({
selector: 'my-duration',
templateUrl: 'duration.component.html',
changeDetection: ChangeDetectionStrategy.OnPush
})
export class DurationComponent implements ControlValueAccessor, OnInit {
@Input() required: boolean;

durations: string[] = [
'15m',
'30m',
'45m',
'1h',
'1h 15m',
'1h 30m',
'1h 45m',
'2h'
];
filteredDurations: Observable<string[]>;

durationControl = new FormControl('', [
Validators.required,
Validators.pattern(Regex.duration),
DurationComponent.zeroDurationValidator
]);

constructor(
@Self()
@Optional()
public ngControl: NgControl
) {
if (ngControl) {
ngControl.valueAccessor = this;
}
}

static zeroDurationValidator(control: AbstractControl): ValidationErrors {
return control.value === '0m' ||
control.value === '00m' ||
control.value === '0h' ||
control.value === '00h' ||
control.value === '0h 0m' ||
control.value === '0h 00m' ||
control.value === '00h 00m' ||
control.value === '00h 0m'
? { zeroDurationError: true }
: null;
}

onChangeCallback = (value: string) => {};
onTouchCallback = () => {};

ngOnInit(): void {
this.initializeFilters();
}

initializeFilters() {
this.filteredDurations = this.durationControl.valueChanges.pipe(
tap(value => this.onChangeCallback(value)),
map(value => this.filterDurations(value))
);
}

onBlur() {
this.onTouchCallback();
}

registerOnChange(fn: any): void {
this.onChangeCallback = fn;
}

registerOnTouched(fn: any): void {
this.onTouchCallback = fn;
}

writeValue(obj: any): void {
this.durationControl.setValue(obj, {
emitModelToViewChange: true
});
this.onChangeCallback(obj);
}

private filterDurations(value: string) {
return this.durations.filter(duration => duration.indexOf(value) === 0);
}
}

duration.component.html

<mat-form-field>
<input [formControl]="durationControl"
type="text"
matInput
autocomplete="off"
[placeholder]="'DURATION' | translate"
[matAutocomplete]="durationAutocomplete"
>
<mat-error *ngIf="durationControl.hasError('required')">{{ 'FORM_VALIDATION.REQUIRED' | translate }}</mat-error>
<mat-error *ngIf="durationControl.hasError('pattern')">{{ 'FORM_VALIDATION.INVALID_FORMAT' | translate }}</mat-error>
<mat-error *ngIf="durationControl.hasError('zeroDurationError')">{{ 'FORM_VALIDATION.ZERO_DURATION_ERROR' | translate }}</mat-error>

<mat-autocomplete #durationAutocomplete="matAutocomplete">
<mat-option *ngFor="let duration of filteredDurations | async" [value]="duration">
{{ duration }}
</mat-option>
</mat-autocomplete>
</mat-form-field>

最佳答案

重置表单时,所有控件在 writeValue 方法中接收 null。然后我可以像这样重置 durationControl:

writeValue(obj: any): void {
if (obj === null) {
this.durationControl.reset();
}

this.durationControl.setValue(obj, {
emitModelToViewChange: true
});
this.onChangeCallback(obj);
}

关于angular - 如何使用 Angular 重置自定义表单控件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50614909/

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