What is the difference between Subject and BehaviorSubject?

A BehaviorSubject holds one value. When it is subscribed it emits the value immediately. A Subject doesn’t hold a value. Subject example (with RxJS 5 API): const subject = new Rx.Subject(); subject.next(1); subject.subscribe(x => console.log(x)); Console output will be empty BehaviorSubject example: const subject = new Rx.BehaviorSubject(0); subject.next(1); subject.subscribe(x => console.log(x)); Console output: 1 In … Read more

*ngIf and *ngFor on same element causing error

Angular v2 doesn’t support more than one structural directive on the same element. As a workaround use the <ng-container> element that allows you to use separate elements for each structural directive, but it is not stamped to the DOM. <ng-container *ngIf=”show”> <div *ngFor=”let thing of stuff”> {{log(thing)}} <span>{{thing.name}}</span> </div> </ng-container> <ng-template> (<template> before Angular v4) … Read more

How to apply filters to *ngFor?

Basically, you write a pipe which you can then use in the *ngFor directive. In your component: filterargs = {title: ‘hello’}; items = [{title: ‘hello world’}, {title: ‘hello kitty’}, {title: ‘foo bar’}]; In your template, you can pass string, number or object to your pipe to use to filter on: <li *ngFor=”let item of items … Read more