7

[angular2 RC4 + @angular/forms module]

I have a component using OnPush change detection containing a FormGroup. This form contains a FormControl with an async validator.

When the validation is complete (no more pending), the view is not refresh. Only the input blur event makes view refresh..

If I remove the OnPush change detection, it works properly.

This plunker demonstrates it.

Is it an angular bug or I'm doing something wrong?

1 Answer 1

7

Looks like you are missing the goal of using ChangeDetectionStrategy.OnPush.

You don't have any @Input property or | async pipe on your component, but it is only point of updating the view state with this strategy - when Input property updated (in ideal with a new object). So just get rid of it for current case

UPDATE

if you still insist on using OnPush in this case, for expected behavior you should trigger change detection manually.

Add import {..., ChangeDetectorRef} from '@angular/core'; and inject instance of it in constructor.

In your method you have to add this:

 uniqueNameAsync(control: FormControl): Promise<{[key: string]:any}>{
   return new Promise(resolve => {
       setTimeout(() =>{
            resolve(null);
            this.changeRef.markForCheck();
      }, 500);
   });
 }
Sign up to request clarification or add additional context in comments.

4 Comments

With OnPush, view refresh is trigger too when events happens inside the component like a click or an input typing. In the plunker, view is refresh when you begin typing ('pending' appears). And sync validators works well.. so I don't know if I can consider it like normal behavior or a bug with async validators..
Ok, so if you still insist on using OnPush in this case, for expected behavior you should trigger change detection manually. Add import {..., ChangeDetectorRef} from '@angular/core'; and inject instance of it in constructor. In your method you have to add this: setTimeout(() =>{ resolve(null); this.changeRef.markForCheck(); }, 500)
Async execution only invokes change detection in OnPush when used with | async which is not possible in this case. I also think you need to invoke change detection manually.
ok thanks guys! I'm using ngrx/store and trying to use onPush almost everywhere. It would be nice to have ng2 forms implemented this under the hood because it could knows 'things happens' inside the component after async validation..

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.