Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reactive forms valueChanges how to watch just 2 fields[Angular2]

Tags:

angular

rxjs

How can I watch just 2 fields of an Angular ReactiveForm? I have a form like the following:

this.filter = this.fb.group({
            text: ['', []],
            bankCountry: ['', []],
            bankCity: ['', []]
        });

How can I watch for changes related to just the last 2 fields?

like image 941
Vladimir Djukic Avatar asked Oct 30 '22 17:10

Vladimir Djukic


1 Answers

Two ways:

1st, using combineLatest:

http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#static-method-combineLatest

Observable.combineLatest( this.text.valueChanges, this.bankCountry.valueChanges ).subscribe(respArr => {
     console.log(respArr); //this is array with responses from both observables
});

2nd, using filter:

http://reactivex.io/rxjs/class/es6/Observable.js~Observable.html#instance-method-filter

this.filter.valueChanges.filter((val) => /*filter predicate returning boolean*/).subscribe(val => {
    console.log(val);
});
like image 108
Maciej Treder Avatar answered Nov 15 '22 05:11

Maciej Treder