gpt4 book ai didi

angular - 如何在 Angular 中将表单控件重置为原始状态?

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

如果我有以下表单控件,

<mat-form-field class="example-full-width">
<input matInput placeholder="First Name" type="text" class="form-control" name="firstName" [(ngModel)]="firstName">
</mat-form-field>

如何从组件将其状态重置回 ng-pristine?

最佳答案

这会将表单中的每个 ngModel 标记为原始。如果您想将特定的原始标记为原始,只需在 forEach 循环中添加一个条件即可

class //bla bla bla {
@ViewChildren(NgModel) fields: QueryList<NgModel>;

// stuffs

checkErrors() {
this.fields.forEach(model => {
model.control.markAsPristine();
}
};

如果您想重置值,只需在原始标记之前添加 model.control.reset(); 即可

关于angular - 如何在 Angular 中将表单控件重置为原始状态?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47775599/

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