I created angular 4 app with following component as below.
<div></div>
<p>d3 works!</p>
<button (click)="getCommitData()">button1</button>
<label style="display: block">{{data}}</label>
import {Component, OnInit} from '@angular/core';
import {HttpModule, Http} from '@angular/http';
import 'rxjs/add/operator/toPromise';
import * as $ from 'jquery';
@Component({
selector: 'app-d3',
templateUrl: './d3.component.html',
styleUrls: ['./d3.component.css']
})
export class D3Component {
user: String = 'angular';
repo: String = 'angular.js';
data: String;
error: String;
url: string;
constructor(private http: Http) {
this.url = 'https://api.github.com/repos/' + this.user +
'/' +
this.repo +
'/commits';
}
humanReadableDate(d: Date): String {
return d.getUTCMonth() + 1 + '/' + d.getUTCDate();
}
reformatGITResponse(data: String[]): any {
return data;
}
handleError(error: any): Promise<any> {
console.error('An error occurred', error); // for demo purposes only
return Promise.reject(error.message || error);
}
getCommitData(): Promise<any> {
return this.http.get(this.url).toPromise()
.then(response => this.reformatGITResponse(response.json().data))
.catch(this.handleError);
}
}
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>AngularAppSimple</title>
<base href="/">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<app-root></app-root>
<app-d3></app-d3>
</body>
</html>
But when i call getCommitData() from component.html I'm getting null for both url and http variables in getCommitData method above. But when initiating constructor is being called and the url variable is being set.
How the type script objects get injected ?
Also how shall i display the data returned from getCommitData method above ?