Is it necessary to unsubscribe from observables created by Http methods to avoid memory leaks?

So the answer is no, you don’t. Ng2 will clean it up itself. The Http service source, from Angular’s Http XHR backend source: Notice how it runs the complete() after getting the result. This means it actually unsubscribes on completion. So you don’t need to do it yourself. Here is a test to validate: fetchFilms() … Read more

How do you POST a FormData object in Angular 2?

Template: <label class=”btn btn-primary”> <input type=”file” style=”display: none;” multiple=”true” (change)=”fileChange($event)” accept=”.xml”> <span>Click Me!</span> </label> UPD: Angular 5 – HttpClient + Typescript onFileUpload(event: EventTarget) { const eventObj: MSInputMethodContext = <MSInputMethodContext>event; const target: HTMLInputElement = <HTMLInputElement>eventObj.target; const files: FileList = target.files; const formData: FormData = new FormData(); for (let i = 0; i < files.length; i++) { … 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

Cannot find module ‘./in-memory-data-service’ in tour of heroes for Angular2

ng generate service InMemoryData –module=app Will create the src/app/in-memory-data.service.ts file. Then add the code listed in the tutorial and it will work. AFAIK they don’t even imply that in the tutorial so don’t feel bad. In fact what they say is The forRoot() configuration method takes an InMemoryDataService class that primes the in-memory database. The … 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 correctly set Http Request Header in Angular 2

Your parameter for the request options in http.put() should actually be of type RequestOptions. Try something like this: let headers = new Headers(); headers.append(‘Content-Type’, ‘application/json’); headers.append(‘authentication’, `${student.token}`); let options = new RequestOptions({ headers: headers }); return this.http .put(url, JSON.stringify(student), options)

Angular – POST uploaded file

Look at my code, but be aware. I use async/await, because latest Chrome beta can read any es6 code, which gets by TypeScript with compilation. So, you must replace asyns/await by .then(). Input change handler: /** * @param fileInput */ public psdTemplateSelectionHandler (fileInput: any){ let FileList: FileList = fileInput.target.files; for (let i = 0, length … Read more

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)