user3384985
user3384985

Reputation: 3017

Angular - get one subscription from multiple

I need to have only latest subscription from multiple.

When my FormControl value changes, I need to have only latest value after user finished typing. Here is my code -

let control = this.register.controls['email'];
control.valueChanges.debounceTime(300).pipe(take(1)).subscribe(newValue => {
  console.log(newValue);
})

But it returns multiple Subscriptions. How can I get latest one?

Thanks!

Upvotes: 0

Views: 282

Answers (2)

rijin
rijin

Reputation: 1759

You can achieve this with withLatestFrom or combineLatest. But recommended way is to use withLatestFrom

  • take(1) will take only the first value and its stops observing.

    this.formGroup.get('nom').valueChanges
    .pipe(
      debounceTime(300),
      // combineLatest()
      withLatestFrom()
    ).subscribe(newValue => {
      console.log(newValue);
    });
    

Checkout: https://stackblitz.com/edit/angular-formcontrol-example-seyfto

Upvotes: 1

Tony
Tony

Reputation: 20092

You can use withlatestfrom

let control = this.register.controls['email'];
control.valueChanges.debounceTime(300).pipe(withLastestForm(control.valueChanges)).subscribe(newValue => {
  console.log(newValue);
})

Change this code to your accordingly

Upvotes: 0

Related Questions