Stop mouse event propagation

If you want to be able to add this to any elements without having to copy/paste the same code over and over again, you can make a directive to do this. It is as simple as below: import {Directive, HostListener} from “@angular/core”; @Directive({ selector: “[click-stop-propagation]” }) export class ClickStopPropagation { @HostListener(“click”, [“$event”]) public onClick(event: any): … 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

Exception: Can’t bind to ‘ngFor’ since it isn’t a known native property

I missed let in front of talk: <div *ngFor=”let talk of talks”> Note that as of beta.17 usage of #… to declare local variables inside of structural directives like NgFor is deprecated. Use let instead. <div *ngFor=”#talk of talks”> now becomes <div *ngFor=”let talk of talks”> Original answer: I missed # in front of talk: … Read more

Angular EXCEPTION: No provider for Http

Import the HttpModule import { HttpModule } from ‘@angular/http’; @NgModule({ imports: [ BrowserModule, HttpModule ], providers: [], declarations: [ AppComponent ], bootstrap: [ AppComponent ] }) export default class AppModule { } platformBrowserDynamic().bootstrapModule(AppModule); Ideally, you split up this code in two separate files. For further information read: https://v2.angular.io/docs/ts/latest/cookbook/rc4-to-rc5.html https://v2.angular.io/docs/ts/latest/guide/ngmodule.html

@ViewChild in *ngIf

Use a setter for the ViewChild: private contentPlaceholder: ElementRef; @ViewChild(‘contentPlaceholder’) set content(content: ElementRef) { if(content) { // initially setter gets called with undefined this.contentPlaceholder = content; } } The setter is called with an element reference once *ngIf becomes true. Note, for Angular 8 you have to make sure to set { static: false }, … Read more

Angular CLI SASS options

Angular CLI version 9 (used to create Angular 9 projects) now picks up style from schematics instead of styleext. Use the command like this: ng config schematics.@schematics/angular:component.style scssand the resulting angular.json shall look like this “schematics”: { “@schematics/angular:component”: { “style”: “scss” } } Other possible solutions & explanations: To create a new project with angular … 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