export class TehranjsApiService { constructor(private http: Http) { } getHeader = () => { let headers = new Headers(); headers.append("Content-Type", 'application/json');
return headers; };
getArticles(limit:number=10) : any { return this.http.get(`${this.BASE_URL}/articles.json?limit=${limit}`,this.getHeader) .map((response: Response) => { // some response manipulation
return response }) .toPromise(); }
import { Component, OnInit } from '@angular/core';import { TehranjsApiService } from '../../../../shared/shared';@Component({ selector: 'app-home', templateUrl: './home.component.html', styleUrls: ['./home.component.scss']})export class HomeComponent implements OnInit { articles: any; constructor(public tJs: TehranjsApiService) { }
ngOnInit() {
this.tJs.getArticles().then( data => { this.articles= data.json().articles; console.log(data.headers.get('link'))} , //success error => console.log('error', error), () => {console.log('call finished')} )
}
}
<!doctype html><html dir="rtl"><head> <meta charset="utf-8"> <title>front</title> <base href="/"> <meta name="viewport" content="width=device-width, initial-scale=1"> <link rel="icon" type="image/x-icon" href="favicon.ico"> <link href="https://cdn.rawgit.com/rastikerdar/shabnam-font/v1.1.0/dist/font-face.css" rel="stylesheet" type="text/css" /> <script src="assets/js/pub.js"></script>
<meta http-equiv="Content-Security-Policy" content="default-src *; script-src 'self' 'unsafe-inline' 'unsafe-eval' 127.0.0.1:* http://localhost:* http://*.google.com http://*.gstatic.com http://*.googleapis.com http://*.firebaseio.com https://*.google.com https://*.gstatic.com https://*.googleapis.com https://*.firebaseio.com https://*.pinsood.com ; img-src * data:; style-src 'self' 'unsafe-inline' 127.0.0.1 https://*.pinsood.com http://*.google.com http://*.gstatic.com http://*.googleapis.com http://*.firebaseio.com https://*.google.com https://*.gstatic.com https://*.googleapis.com https://*.firebaseio.com http://*.lorempixel.com"></head><body> <app-root>Loading...</app-root></body></html>
return this.http.get(`${this.BASE_URL}/articles.json?limit=${limit}`,this.getHeader)
.map((response: Response) => {
return this.http.get(`${this.BASE_URL}/articles.json?limit=${limit}`,this.getHeader)
.do((r:response)=> console.log('ok',r.json())
.catch(err => {console.error(err); throw new Error(err)})
.map((response: Response) => {