Javascript 在Angular2中更新视图
在angular 2中是新手,这里面临一个问题。在我的项目中有一个加载js文件的API。问题是,我必须使用这个js文件,而不是在我的控制下更改它,基本上,这个js文件有几个方法可以通过AJAX调用API并返回数组,但是它返回它的方式非常陈旧。 我所需要的就是在数据一进来就更新视图,尝试了很多方法,但仍然无法让它工作。有什么办法可以绑定和更新视图吗Javascript 在Angular2中更新视图,javascript,jquery,ajax,angular,Javascript,Jquery,Ajax,Angular,在angular 2中是新手,这里面临一个问题。在我的项目中有一个加载js文件的API。问题是,我必须使用这个js文件,而不是在我的控制下更改它,基本上,这个js文件有几个方法可以通过AJAX调用API并返回数组,但是它返回它的方式非常陈旧。 我所需要的就是在数据一进来就更新视图,尝试了很多方法,但仍然无法让它工作。有什么办法可以绑定和更新视图吗 从“@angular/core”导入{Component,AfterContentInit}; 从“/product”导入{IPProduct};
从“@angular/core”导入{Component,AfterContentInit};
从“/product”导入{IPProduct};
从“/product.service”导入{ProductService};
声明var ExpressLibraryLoader:任何;
@组成部分({
选择器:“pm产品”,
moduleId:module.id,
templateUrl:'product list.component.html',
样式URL:['product-list.component.css']
})
导出类ProductListComponent实现AfterContentInit{
pageTitle:字符串='产品列表';
listFilter:string='';
产品=[];
错误消息:字符串;
构造函数(private\u productService:productService){
}
ngAfterContentInit():void{
//这是.\u productService.getProducts()。
//订阅(products=>this.products=products,error=>this.errorMessage=error);
ExpressLibraryLoader.initialise({
平台键:“xxx”,
布兰迪:20,
图书馆URL:“http://localhost/xxxxcx/",
域启动URL:“http://localhost/xcxcxc/",
成功:函数(ExpressLibrary){
let参数=
{
语言代码:“en”,
gameProviderID:[7],
成功:功能(响应){
如果(response.Status==“Success”){
//response.Result.Games.map(response=>this.products=response);
this.products=response.Result.Games;
console.log(this.products)
}否则{
}
},
错误:函数(){
}
};
ExpressLibrary.games.getDesktop(参数);
}
});
}
}
看起来您需要使用箭头函数,此
引用未指向您的类实例:
success: function (ExpressLibrary) { ... }
//change to
success: (ExpressLibrary) => { ... }
// or, quite funny you mentioned it
success: function (ExpressLibrary) { ... }.bind(this)
看起来您需要使用箭头函数,
此
引用未指向您的类实例:
success: function (ExpressLibrary) { ... }
//change to
success: (ExpressLibrary) => { ... }
// or, quite funny you mentioned it
success: function (ExpressLibrary) { ... }.bind(this)
我发现了这个问题,似乎
这个
在
success: function (response) {
if (response.Status === "Success") {
this.products = response.Result.Games;
console.log(this.products)
} }
有不同的范围。因此,解决方案显而易见且非常简单:
var that = this;
var parameters =
{
//various parameters
success: function(response){
if (response.Status === "Success") {
that.products = response.Result.Games;
}
},
error: function () {
}
};
ExpressLibrary.games.getDesktop(parameters);
我发现了这个问题,似乎
这个
在
success: function (response) {
if (response.Status === "Success") {
this.products = response.Result.Games;
console.log(this.products)
} }
有不同的范围。因此,解决方案显而易见且非常简单:
var that = this;
var parameters =
{
//various parameters
success: function(response){
if (response.Status === "Success") {
that.products = response.Result.Games;
}
},
error: function () {
}
};
ExpressLibrary.games.getDesktop(parameters);
你可能在找ngZone。试试这里:你可能正在寻找ngZone。试试这里:不幸的是,它不起作用。现在尝试使用zone,但运气不佳。不幸的是,它没有起作用。现在正在尝试zone,但还没有运气。