My Code Component
import { Component, OnInit } from '@angular/core'; import { Http } from '@angular/http'; @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 am curious...
I attached the Click event to the addEventListener method.
Why is the http object undefined inside the function? I am unsure of the cause.
I need to access http within the function. Is there a solution?