Access multiple viewchildren using @viewchild

Use @ViewChildren from @angular/core to get a reference to the components template <div *ngFor=”let v of views”> <customcomponent #cmp></customcomponent> </div> component import { ViewChildren, QueryList } from ‘@angular/core’; /** Get handle on cmp tags in the template */ @ViewChildren(‘cmp’) components:QueryList<CustomComponent>; ngAfterViewInit(){ // print array of CustomComponent objects console.log(this.components.toArray()); } l̶i̶v̶e̶ ̶d̶e̶m̶o̶

Passive Link in Angular 2 – equivalent

If you have Angular 5 or above, just change <a href=”” (click)=”passTheSalt()”>Click me</a> into <a [routerLink]=”” (click)=”passTheSalt()”>Click me</a> A link will be displayed with a hand icon when hovering over it and clicking it won’t trigger any route. Note: If you want to keep the query parameters, you should set queryParamsHandling option to preserve: <a … Read more

Set default host and port for ng serve in config file

Angular CLI 6+ In the latest version of Angular, this is set in the angular.json config file. Example: { “$schema”: “./node_modules/@angular/cli/lib/config/schema.json”, “projects”: { “my-project”: { “architect”: { “serve”: { “options”: { “port”: 4444 } } } } } } You can also use ng config to view/edit values: ng config projects[“my-project”].architect[“serve”].options {port:4444} Angular CLI <6 … Read more

How to truncate text in Angular2?

Two way to truncate text into angular. let str=”How to truncate text in angular”; 1. Solution {{str | slice:0:6}} Output: how to If you want to append any text after slice string like {{ (str.length>6)? (str | slice:0:6)+’..’:(str) }} Output: how to… 2. Solution(Create custom pipe) if you want to create custom truncate pipe import … Read more

How to check if Angular application running in Production or Development mode

You can use this function isDevMode import { isDevMode } from ‘@angular/core’; … export class AppComponent { constructor() { console.log(isDevMode()); } } One note: be carefull with this function if(isDevMode()) { enableProdMode(); } You will get Error: Cannot enable prod mode after platform setup https://github.com/angular/angular/blob/2.0.0/modules/%40angular/core/src/application_ref.ts#L58 Other options environment variable import { environment } from ‘src/environments/environment’; … Read more