Get Image or byte data with http

It is not necessary to extend BrowserXhr anymore. (Tested with angular 2.2.1) RequestOptionsArgs now has a property responseType: ResponseContentType which can be set to ResponseContentType.Blob Using DomSanitizer import {DomSanitizer} from ‘@angular/platform-browser’; This example also creates a sanitized url that can be bound to the src property of an <img> this.http.get(url, { headers: {‘Content-Type’: ‘image/jpg’}, responseType: … Read more

Angular 2 – Routing – CanActivate work with Observable

You should upgrade “@angular/router” to the latest . e.g.”3.0.0-alpha.8″ modify AuthGuard.ts @Injectable() export class AuthGuard implements CanActivate { constructor(private loginService: LoginService, private router: Router) {} canActivate(next: ActivatedRouteSnapshot, state: RouterStateSnapshot) { return this.loginService .isLoggedIn() .map((e) => { if (e) { return true; } }) .catch(() => { this.router.navigate([‘/login’]); return Observable.of(false); }); } } If you have … Read more

How to use/import http module?

Last update: May 11, 2016 Angular version: 2.0.0-rc.2 Typescript version: 1.8.10 Live working example. A simple example of how to use the Http module with Observable: import {bootstrap} from ‘@angular2/platform-browser-dynamic’; import {Component, enableProdMode, Injectable, OnInit} from ‘@angular/core’; import {Http, Headers, HTTP_PROVIDERS, URLSearchParams} from ‘@angular/http’; import ‘rxjs/add/operator/map’; const API_KEY = ‘6c759d320ea37acf99ec363f678f73c0:14:74192489’; @Injectable() class ArticleApi { constructor(private … 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

How to use angular2 http API for tracking upload/download progress

As of Angular 4.3.x and beyond versions, it can be achieved using the new HttpClient from @angular/common/http. Read the Listening to progress events section. Simple upload example (copied from the section mentioned above): const req = new HttpRequest(‘POST’, ‘/upload/file’, file, { reportProgress: true, }); http.request(req).subscribe(event => { // Via this API, you get access to … Read more