question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How do you listen for dirty/pristine state of the form control?

See original GitHub issue

I’ve posted this question on StackOverflow recently: How do I know when custom form control is marked as pristine in Angular?.

After further investigation, I’ve managed to get access to both NgModel and FormControl from my custom form component, but I can’t find any observable that I can use to actually listen for dirty/pristine state of the form control. The ControlValueAccessor is not providing any hooks for this task either.

Is it possible to watch for dirty/pristine state of the form control somehow? There must be an observable for this?

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Reactions:24
  • Comments:28 (6 by maintainers)

github_iconTop GitHub Comments

24reactions
Toxicablecommented, Jun 24, 2017

You can listen to an AbstractControl#statusChanges eg, control.statusChanges.map( state => if(state == 'VALID" { //do something })

13reactions
slavafomincommented, Jul 23, 2018

I’ve came up with two possible workarounds for the original issue:

Monkey-patching the markAsPristine()

@Component({
  selector: 'my-custom-form-component',
  templateUrl: './custom-form-component.html',
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: MyCustomFormComponent,
    multi: true
  }]
})
export class MyCustomFormComponent implements ControlValueAccessor, OnInit {

  private control: AbstractControl;


  ngOnInit () {
    const self = this;
    const origFunc = this.control.markAsPristine;
    this.control.markAsPristine = function () {
      origFunc.apply(this, arguments);
      console.log('Marked as pristine!');
    }
  }

}

Watching for changes with ngDoCheck

@Component({
  selector: 'my-custom-form-component',
  templateUrl: './custom-form-component.html',
  providers: [{
    provide: NG_VALUE_ACCESSOR,
    useExisting: MyCustomFormComponent,
    multi: true
  }]
})
export class MyCustomFormComponent implements ControlValueAccessor, DoCheck {

  private control: AbstractControl;

  private pristine = true;


  ngDoCheck (): void {
    if (this.pristine !== this.control.pristine) {
      this.pristine = this.control.pristine;
      if (this.pristine) {
        console.log('Marked as pristine!');
      }
    }
  }

}

I’ve also updated the questions on StackOveflow:

Read more comments on GitHub >

github_iconTop Results From Across the Web

How to listen on dirty/pristine status in Angular 6
You don't need each such call - only the first one (when the form GETS DIRTY). So it's not the best approach you...
Read more >
Monkey Patching A Touched/Dirty/Pristine Event Listener In ...
To my surprise, there is currently no event that is emitted when a control is touched – or when it's status of pristine/dirty...
Read more >
Input controls remain dirty and not pristine after form.reset() in ...
When calling form.reset() after changing a controls value, I would expect the controls to be reset to dirty: false, pristine: true, touched: false....
Read more >
Angular Form Pristine, Dirty, Touched & Status - YouTube
Angular Tutorial:#angular #angulartutorial #nitishkaushikGitHub Repo: ...
Read more >
Detect Unsaved Changes in Angular Forms | by Netanel Basal
When the beforeunload event emits, we check if the component is dirty. If it is, we set the returnValue to false, which will...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found