Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect/watch "dirty-status" of an angular2-form in the right way?

Tags:

forms

angular

I have a form in Angular2 (e.g)

<form id="myLovelyForm" name="myLovelyForm" #myLovelyForm="ngForm">
    <label [attr.for]="myLovelyCheckbox">
      <input [attr.id]="myLovelyCheckbox" type="checkbox"
             [(ngModel)]="myLovelyCheckbox">
      <span class="myLovelyCheckbox">myLovelyCheckbox</span>
    </label>
</form>

and an animation, which should start, if the form is dirty:

<div 
    id="myLovelyNotification" 
    class="myLovelyNotification" 
    [@notification]="myLovelyForm.form.dirty">
.....
.....
</div>

The animation works properly if I set [@notification] = true, but my myLovelyForm.dirty does not fire, if I touch the form and change an element.

If the @notification is false, the animation stops, i.e. if the checkbox was selected before and I unselect it mistakenly and select it again, the form is not pristine (touched) but not dirty anymore, therefore the animation should stop. If I set the @notification = false manually, it works properly.

The big question is: How can I detect/watch "dirty-status" of an angular2-form in the right way?

like image 218
Lonely Avatar asked Sep 26 '16 09:09

Lonely


People also ask

What is angular form dirty?

When the user changes the value in the watched field, the control is marked as "dirty" When the user blurs the form control element, the control is marked as "touched"

How do you make angular forms dirty?

You should use the markAsDirty method, like this: control. markAsDirty(); This will also mark all direct ancestors as dirty to maintain the model.

How to use reactive forms in angular?

To use the Angular forms, First, we need to import the FormsModule (for template-driven forms) & ReactiveFormsModule ( for Reactive Forms) from the @angular/forms in your route module. Also, add it to the imports metadata In Reactive forms, we create the form model in the component class.

What is the issue number for angular on GitHub?

· Issue #11912 · angular/angular · GitHub Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community. By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails. Already on GitHub?

How many top-level form groups are there in angular?

Every Angular Form must have at least one top-level FormGroup. It tracks the value & validity of the entire form. The Angular has two approaches to building the Angular Forms. One is Template-driven and the other one is Reactive Forms.

How to check if submodule is clean or dirty?

STATUS="dirty" else echo "Submodule $ {name} is clean." fi' echo -n "Main repository is clean " if [ "$ {STATUS}" = "dirty" ]; then echo "- but at least one submodule is dirty."


2 Answers

Simply -

@ViewChild('f') templateForm: any;


ngOnInit() {
    this.templateForm.valueChanges.subscribe((value: any) => {
        if (this.templateForm.dirty) {
            console.log('template form dirty - yes: ', value);
        } else {
            console.log('template form dirty - no: ');
        }
    });
}

Where your template contains:

 <form  #f="ngForm" (ngSubmit)="save(f)>
    ...
 </form>

However this is still using template forms which are really there to help bridge the gap with Angular1 apps. Model Driven forms are the Angular 2 way of doing it for anything but real basic applications. See for example:

  • https://blog.thoughtram.io/angular/2016/06/22/model-driven-forms-in-angular-2.html
  • http://blog.ng-book.com/the-ultimate-guide-to-forms-in-angular-2/
  • And use custom components to really extend and excell your app - https://blog.thoughtram.io/angular/2016/07/27/custom-form-controls-in-angular-2.html
like image 80
HankCa Avatar answered Oct 12 '22 12:10

HankCa


You can subscribe to form changes:

this.physicalForm.valueChanges
   .map((value) => {
      return value;
   })
   .subscribe((value) => {
      if(this.selectedPhysical.weight != this.physicalForm.value.weight)  {      
        this.selectedPhysical.weight = this.physicalForm.value.weight;
      }
      this.isDirty == this.physicalForm.touched;
   });

If this event fires, then you know your form is dirty.

this is an example from my actual app (nut.abbr is the formcontrolName):

ngOnInit() {
   for (let nut of this.userSettings.nutrientData) {
      this.foodSettingsForm.controls[nut.abbr].valueChanges
         .subscribe(v => { console.log("value: ", v); this.completeValueChange(nut.abbr, v); });
   }
}

completeValueChange(field: string, value: boolean) {
   this.isChanged = true;
   Nutrient.updateNutrient(field, value, this.userSettings.nutrientData);
}
like image 22
John Baird Avatar answered Oct 12 '22 11:10

John Baird