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.

ng 11: patchValue, valueChanges & async pipe

See original GitHub issue

🐞 bug report

I don’t know if this is a bug or feature.

  • I have AbstractControl for the input field on which I want to listen to valueChanges with async pipe.
  • It works fine if user write to the input field async pipe work
  • If I programatacly patch value then async pipe doesn’t work
  • code ts:
searchForm: FormGroup;
...
ngOnInit(): void {
    this.initSearchForm();

    this.navigateBack = this.route.snapshot.queryParams.back;
    if (this.navigateBack && this.navigateBack === 'true') {
      this.orgCtrl.patchValue(null);
      // this.orgCtrl.setValue(this.orgStorage.getSearchKey(), { emitEvent: false, onlySelf: true });
      this.orgCtrl.setValue(this.orgStorage.getSearchKey());
      this.result$ = of(this.orgStorage.getSearchResult());
    }
  }

get orgCtrl(): AbstractControl { return this.searchForm.get('org'); }

private initSearchForm(): void {
    this.searchForm = this.formBuilder.group({ org: '' });
    this.result$ = this.orgCtrl.valueChanges
      .pipe(
        filter(v => v.length >= 3),
        tap(v => console.log(v)),
        switchMap(v => this.search(v))
      );
  }
  • HTML code:
<div *ngIf="orgCtrl?.valueChanges | async">
    <p *ngFor="let r of result$ | async">
        {{r.name}}
    </p>
</div>

Affected Package

Is this a regression?

I don’t know it is a fresh projcet with ng 11

Description

  • When pectValue with a reactive form async pipe doesn’t detected valueChanges.

🔬 Minimal Reproduction

🔥 Exception or Error

There is no error in the browser dev console.

🌍 Your Environment

Angular Version:


Angular CLI: 11.0.1
Node: 12.6.0
OS: win32 x64

Angular: 11.0.0
... animations, common, compiler, compiler-cli, core, forms
... language-service, localize, platform-browser
... platform-browser-dynamic, router
Ivy Workspace: Yes

Package                         Version
---------------------------------------------------------
@angular-devkit/architect       0.1100.1
@angular-devkit/build-angular   0.1100.1
@angular-devkit/core            11.0.1
@angular-devkit/schematics      11.0.1
@angular/cli                    11.0.1
@schematics/angular             11.0.1
@schematics/update              0.1100.1
rxjs                            6.6.3
typescript                      4.0.5

Anything else relevant?

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:2
  • Comments:15 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
Totaticommented, Nov 18, 2020

Hi, we use a helper valuesOf function, that wraps the AbstractControl if we want to have an observable stream with the current value at subscription. In the template using just ctrl.valueChnages | async is a bit overkill, and most of the time can be replaced with a simple ctrl.value. If you want to pipe the valueChanges, then using valuesOf is even less code. Example

export function valuesOf(control: AbstractControl): Observable<any>;
export function valuesOf<T>(control: AbstractControl): Observable<T | null>;
export function valuesOf(control: AbstractControl): Observable<any> {
  return defer(() => control.valueChanges.pipe(startWith(control.value)));
}
1reaction
petebacondarwincommented, Nov 16, 2020

OK, so it appears to be a problem with timing. If you create the form in ngOnInit() but then do the patchValue() call in ngAfterViewInit() then it works: https://stackblitz.com/edit/angular-ivy-6jxkwu?file=src%2Fapp%2Fapp.component.ts

Read more comments on GitHub >

github_iconTop Results From Across the Web

Async Pipe all the things! - Medium
Here's a quick tutorial of how you can easily subscribe to every Observable with the async pipe, and never again worry about unsubscribing....
Read more >
angular - How to fire the `valueChanges` programmatically?
I use patchValue to reset the value in the form and trigger the change programmatically: this.
Read more >
Updating Angular Forms with patchValue or setValue
In this case, I've been using AngularFire2 which returns a FirebaseObjectObservable , therefore I can pipe it through switchMap and get the data ......
Read more >
FormControl - Angular
It exists for symmetry with patchValue on FormGroups and FormArrays , where ... how the control propagates changes and emits events after the...
Read more >
Testing complex forms - Testing Angular
The asynchronous validators use the SignupService to talk to the back-end ... They are passed to ControlErrorsComponent as an ng-template .
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

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