My Component Source
import { Component, OnInit } from '@angular/core'; import { Http } from '@angular/http'; //import { DatatableComponent } from '../../shared/ui/datatable/datatable.component' @Component({ selector: 'app-root', template: '<button id="testBtn"></button>' }) export class MarketComponent implements OnInit { constructor( public http: Http ) { } ngOnInit() { let btn = document.getElementById("testBtn"); if(btn){ btn.addEventListener('click', function(){ this.http.get('./test.json') .map(response=>response.json()) .subscribe( data => { //success console.log("success"); }, error => console.log("HttpRequestButton Error"), //error () => console.log("CategoryCount HttpRequestButton Get Test Finish") ); } ); } // if end } }
I wonder...
I added the Click event to the addEventListener method.
The http used in the function is undefined. Why? I do not know the reason.
I have to use http inside the function. Is there a way?