Triggering change detection manually in Angular

Try one of these: ApplicationRef.tick() – similar to AngularJS’s $rootScope.$digest() — i.e., check the full component tree NgZone.run(callback) – similar to $rootScope.$apply(callback) — i.e., evaluate the callback function inside the Angular zone. I think, but I’m not sure, that this ends up checking the full component tree after executing the callback function. ChangeDetectorRef.detectChanges() – similar … Read more

Equivalent of $compile in Angular 2

Angular 2.3.0 (2016-12-07) To get all the details check: How can I use/create dynamic template to compile dynamic Component with Angular 2.0? To see that in action: observe a working plunker (working with 2.3.0+) The principals: 1) Create Template 2) Create Component 3) Create Module 4) Compile Module 5) Create (and cache) ComponentFactory 6) use … Read more

How can I select an element in a component template?

Instead of injecting ElementRef and using querySelector or similar from there, a declarative way can be used instead to access elements in the view directly: <input #myname> @ViewChild(‘myname’) input; element ngAfterViewInit() { console.log(this.input.nativeElement.value); } StackBlitz example @ViewChild() supports directive or component type as parameter, or the name (string) of a template variable. @ViewChildren() also supports … Read more

What is the correct way to share the result of an Angular Http network call in RxJs 5?

EDIT: as of 2021, the proper way is to use the shareReplay operator natively proposed by RxJs. See more details in below answers. Cache the data and if available cached, return this otherwise make the HTTP request. import {Injectable} from ‘@angular/core’; import {Http, Headers} from ‘@angular/http’; import {Observable} from ‘rxjs/Observable’; import ‘rxjs/add/observable/of’; //proper way to … Read more

How can I use/create dynamic template to compile dynamic Component with Angular 2.0?

EDIT – related to 2.3.0 (2016-12-07) NOTE: to get solution for previous version, check the history of this post Similar topic is discussed here Equivalent of $compile in Angular 2. We need to use JitCompiler and NgModule. Read more about NgModule in Angular2 here: Angular 2 RC5 – NgModules, Lazy Loading and AoT compilation In … Read more

Angular/RxJS When should I unsubscribe from `Subscription`

TL;DR For this question there are two kinds of Observables – finite value and infinite value. http Observables produce finite (1) values and something like a DOM event listener Observable produces infinite values. If you manually call subscribe (not using async pipe), then unsubscribe from infinite Observables. Don’t worry about finite ones, RxJs will take … Read more

Dynamic tabs with user-click chosen components

update Angular 5 StackBlitz example update ngComponentOutlet was added to 4.0.0-beta.3 update There is a NgComponentOutlet work in progress that does something similar https://github.com/angular/angular/pull/11235 RC.7 Plunker example RC.7 // Helper component to add dynamic components @Component({ selector: ‘dcl-wrapper’, template: `<div #target></div>` }) export class DclWrapper { @ViewChild(‘target’, {read: ViewContainerRef}) target: ViewContainerRef; @Input() type: Type<Component>; cmpRef: … Read more