Merge subarrays using Observables

Provided it is a typo and the second element has subItems as well (and not searchProfiles), you don’t need flatMap or any thing of the sort, you can do it in a plain map using js array operators: var transformed = [].concat(…result.map(item => item.subItems)); or in your case httpResult$.map(result => [].concat(…result.map(item => item.subItems)) if the … Read more

Best way to import Observable from rxjs

Rxjs v 6.* It got simplified with newer version of rxjs . 1) Operators import {map} from ‘rxjs/operators’; 2) Others import {Observable,of, from } from ‘rxjs’; Instead of chaining we need to pipe . For example Old syntax : source.map().switchMap().subscribe() New Syntax: source.pipe(map(), switchMap()).subscribe() Note: Some operators have a name change due to name collisions … Read more

Testing error case with observables in services

You can simply mock Observable throw error object like Observable.throw({status: 404})and test error block of observable. const xService = fixture.debugElement.injector.get(SomeService); const mockCall = spyOn(xService, ‘method’).and.returnValue(Observable.throw({status: 404})); Update 2019 : Since some people are lazy to read comment let me put this here : It’s a best practice to use errors for Rxjs import { throwError … Read more

Subject vs BehaviorSubject vs ReplaySubject in Angular

It really comes down to behavior and semantics. With a Subject – a subscriber will only get published values that were emitted after the subscription. Ask yourself, is that what you want? Does the subscriber need to know anything about previous values? If not, then you can use this, otherwise choose one of the others. … Read more

Angular 2+ and Observables: Can’t bind to ‘ngModel’ since it isn’t a known property of ‘select’

>= RC.5 The FormsModule needs to be imported to make ngModel available @NgModule({ imports: [BrowserModule /* or CommonModule */, FormsModule, ReactiveFormsModule], … )} class SomeModule {} <= RC.4 In config.js add ‘@angular/forms’: { main: ‘bundles/forms.umd.js’, defaultExtension: ‘js’ }, in main.ts add import {provideForms, disableDeprecatedForms} from ‘@angular/forms’; bootstrap(App, [disableDeprecatedForms(),provideForms()]) Plunker example See also https://angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html

take(1) vs first()

Operators first() and take(1) aren’t the same. The first() operator takes an optional predicate function and emits an error notification when no value matched when the source completed. For example this will emit an error: import { EMPTY, range } from ‘rxjs’; import { first, take } from ‘rxjs/operators’; EMPTY.pipe( first(), ).subscribe(console.log, err => console.log(‘Error’, … Read more