【发布时间】:2016-10-03 13:46:52
【问题描述】:
我想在我用 typescript 编写的 angular2 应用程序中加载一个本地 JSON 文件以进行测试。我有 1) 包含 jQuery.d.ts 文件,并尝试使用 $.getJson() 2) 尝试使用带有 async:false 的 $.ajax()。这两个问题都是我不能调用回调函数之外的函数。
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");
}
}
抛出此错误异常:TypeError:this.hello is not a function。 (在 'this.hello()' 中,'this.hello' 是未定义的)
我还注意到类型脚本中内置了一个名为 ajaxGetJSON 的函数。但是我的 IDE 没有提供有关如何使用此功能的文档,并且我无法在线找到它的文档。对这个问题的任何帮助都会很棒。
【问题讨论】:
-
你可以使用
$(document).ready(() => {和success: (data) => {。
标签: jquery json typescript angular