Angular and Typescript: Can’t find names – Error: cannot find name

There is a work-around mentioned in the changelog for 2.0.0-beta.6 (2016-02-11) (listed under breaking changes): If you use –target=es5, you will need to add a line somewhere in your application (for example, at the top of the .ts file where you call bootstrap): ///<reference path=”node_modules/angular2/typings/browser.d.ts”/> (Note that if your file is not in the same … Read more

How to set environment via `ng serve` in Angular 6

You need to use the new configuration option (this works for ng build and ng serve as well) ng serve –configuration=local or ng serve -c local If you look at your angular.json file, you’ll see that you have finer control over settings for each configuration (aot, optimizer, environment files,…) “configurations”: { “production”: { “optimization”: true, … Read more

Difference between HttpModule and HttpClientModule

Use the HttpClient class from HttpClientModule if you’re using Angular 4.3.x and above: import { HttpClientModule } from ‘@angular/common/http’; @NgModule({ imports: [ BrowserModule, HttpClientModule ], … class MyService() { constructor(http: HttpClient) {…} It’s an upgraded version of http from @angular/http module with the following improvements: Interceptors allow middleware logic to be inserted into the pipeline … Read more

When to use Interface and Model in TypeScript / Angular

Interfaces are only at compile time. This allows only you to check that the expected data received follows a particular structure. For this you can cast your content to this interface: this.http.get(‘…’) .map(res => <Product[]>res.json()); See these questions: How do I cast a JSON object to a typescript class How to get Date object from … Read more

Angular2 material dialog has issues – Did you add it to @NgModule.entryComponents?

Angular 9.0.0 < Since 9.0.0 with Ivy, the entryComponents property is no longer necessary. See deprecations guide. Angular 9.0.0 > You need to add dynamically created components to entryComponents inside your @NgModule @NgModule({ declarations: [ AppComponent, LoginComponent, DashboardComponent, HomeComponent, DialogResultExampleDialog ], entryComponents: [DialogResultExampleDialog] Note: In some cases entryComponents under lazy loaded modules will not work, … Read more

How to reload the current route with the angular 2 router

Create a function in the controller that redirects to the expected route like so redirectTo(uri:string){ this.router.navigateByUrl(“https://stackoverflow.com/”, {skipLocationChange: true}).then(()=> this.router.navigate([uri])); } then use it like this this.redirectTo(‘//place your uri here’); this function will redirect to a dummy route and quickly return to the destination route without the user realizing it.

@HostBinding and @HostListener: what do they do and what are they for?

A quick tip that helps me remember what they do – HostBinding(‘value’) myValue; is exactly the same as [value]=”myValue” And HostListener(‘click’) myClick(){ } is exactly the same as (click)=”myClick()” HostBinding and HostListener are written in directives and the other ones (…) and [..] are written inside templates (of components).