Angular 2 Testing – Async function call – when to use

async will not allow the next test to start until the async finishes all its tasks. What async does is wrap the callback in a Zone, where all asynchronous tasks (e.g. setTimeout) are tracked. Once all the asynchronous tasks are complete, then the async completes. If you have ever worked with Jasmine outside out Angular, … Read more

Angular show spinner for every HTTP request with very less code changes

@jornare has a good idea in his solution. He’s handling the case for multiple requests. However, the code could be written simpler, without creating new observable and storing requests in memory. The below code also uses RxJS 6 with pipeable operators: import { Injectable } from ‘@angular/core’; import { HttpRequest, HttpHandler, HttpInterceptor, HttpResponse } from … Read more

Set base url for angular 2 http requests

For angular 4.3+ and @angular/common/http It’s can be done with interceptors @Injectable() export class ExampleInterceptor implements HttpInterceptor { intercept(req: HttpRequest<any>, next: HttpHandler): Observable<HttpEvent<any>> { const url=”http://myurl.com”; req = req.clone({ url: url + req.url }); return next.handle(req); } } app.module.ts import { NgModule } from ‘@angular/core’; import { Injectable } from ‘@angular/core’; import { HttpClientModule, HttpRequest, … Read more

Angular 6 MatTable Performance in 1000 rows

Not sure if this will help your situation as there’s no code but we’ve found that the MatTable loads very slowly if a large data set is set before you set the datasource paginator. For example – this takes several seconds to render… dataSource: MatTableDataSource<LocationItem> = new MatTableDataSource(); @ViewChild(MatSort) sort: MatSort; @ViewChild(MatPaginator) paginator: MatPaginator; ngOnInit() … Read more

Testing error case with observables in services

You can simply mock Observable throw error object like Observable.throw({status: 404})and test error block of observable. const xService = fixture.debugElement.injector.get(SomeService); const mockCall = spyOn(xService, ‘method’).and.returnValue(Observable.throw({status: 404})); Update 2019 : Since some people are lazy to read comment let me put this here : It’s a best practice to use errors for Rxjs import { throwError … Read more

Multiple conditions in ngClass – Angular 4

You are trying to assign an array to ngClass, but the syntax for the array elements is wrong since you separate them with a || instead of a ,. Try this: <section [ngClass]=”[menu1 ? ‘class1’ : ”, menu2 ? ‘class1’ : ”, (something && (menu1 || menu2)) ? ‘class2’ : ”]”> This other option should … Read more

Angular2 get clicked element id

If you want to have access to the id attribute of the button you can leverage the srcElement property of the event: import {Component} from ‘angular2/core’; @Component({ selector: ‘my-app’, template: ` <button (click)=”onClick($event)” id=”test”>Click</button> ` }) export class AppComponent { onClick(event) { var target = event.target || event.srcElement || event.currentTarget; var idAttr = target.attributes.id; var … Read more