简体   繁体   English

如何检查Angular 5模态形式的脏或原始?

[英]How to check dirty or pristine in Angular 5 modal form?

I am working in a form in a modal. 我正在以模态中的形式工作。 I want to submit form only if the there are any change made to the form field. 我想仅在表单字段有任何更改时才提交表单。 My form looks like below: 我的表单如下所示:

HTML HTML

<form [formGroup]="productForm" *ngIf="productForm"
(ngSubmit)="submitUpdatedRecord(productForm.value)">

    <input
    type="text" 
    class="form-control" 
    formControlName="product_code" 
    name="product_code_clip"
    placeholder="Enter Product Code Clip"
    [(ngModel)]="editProductData.product_code" />

    <button type="submit" class="btn btn-block btn-update" [disabled]=!productForm.valid>
    UPDATE
    </button>
</form>

TS TS

submitUpdatedRecord(formData: NgForm) {
        console.log(formData.dirty);
        return false;

I did ng-pristine check in Angular JS but I am not sure how to implement it with Angular 5? 我在Angular JS中进行了ng-pristine检查,但我不确定如何使用Angular 5实现它?

Can anybody please help? 有人可以帮忙吗?

Seems like you have a mix of Reactive and Template driven forms. 好像你混合了Reactive和Template驱动的表单。 I am not sure why you need [(ngModel)] . 我不确定你为什么需要[(ngModel)] pristine and dirty are also available in Angular 5+. Angular 5+也提供pristinedirty But you need to access it from your form group. 但是您需要从表单组访问它。

submitUpdatedRecord() {
   console.log(this.productForm.pristine);
}

FormGroup extends AbstractControl. FormGroup扩展了AbstractControl。 You can find more information about available properties here . 您可以在此处找到有关可用属性的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM