I want to load a local JSON file for testing purposes in my angular2 app written in typescript. I have 1) Included the jQuery.d.ts file, and tried to use $.getJson() 2) tried $.ajax() with async:false. The problem with both of these is that I cannot call functions outside of the callback function.
ngOnInit():any {
$(document).ready(function(){
$.ajax({
async: false,
url: "../jsonfile/guestRecommendations/1.json",
dataType: "json",
method: "GET",
success: function (data) {
this.hello();
}
});
alert(this.json_data);
})
this.json_data = "before";
}
hello(){
alert("hello");
}
}
This errror is thrown EXCEPTION: TypeError: this.hello is not a function. (In 'this.hello()', 'this.hello' is undefined)
I have also noticed that there is a function built into type script called ajaxGetJSON. But my IDE does not provides docs for how to use this function and I cannot find docs online for it. Any help with this question would be great.