Angular 2 中的新功能,在这里面临一个问题。有一个在我的项目中加载 js 文件的 API。问题是,我必须使用这个 js 文件,并且不受我的控制来更改它,基本上,这个 js 文件有几个通过 AJAX 调用 API 并返回数组的方法,但它返回它的方式相当老式。 我所需要的只是在数据进来后立即更新 View ,尝试了很多方法,但仍然无法使其工作。有什么想法如何绑定(bind)和更新 View 吗?
import {Component, AfterContentInit} from "@angular/core";
import {IProduct} from "./product";
import {ProductService} from "./product.service";
declare var ExpressLibraryLoader: any;
@Component({
selector: 'pm-products',
moduleId: module.id,
templateUrl: 'product-list.component.html',
styleUrls: ['product-list.component.css']
})
export class ProductListComponent implements AfterContentInit{
pageTitle: string = 'Product List';
listFilter: string = '';
products = [];
errorMessage: string;
constructor(private _productService: ProductService) {
}
ngAfterContentInit(): void{
//this._productService.getProducts().
// subscribe(products => this.products = products, error => this.errorMessage = <any>error);
ExpressLibraryLoader.initialise({
platformKey: 'xxx',
brandID: 20,
libraryUrl: "http://localhost/xxxxcx/",
domainLaunchUrl: "http://localhost/xcxcxc/",
success: function (ExpressLibrary) {
let parameters =
{
languageCode: 'en',
gameProviderID: [7],
success: function (response) {
if (response.Status === "Success") {
//response.Result.Games.map(response => this.products = response);
this.products = response.Result.Games;
console.log(this.products)
} else {
}
},
error: function () {
}
};
ExpressLibrary.games.getDesktop(parameters);
}
});
}
}
最佳答案
看起来您需要使用箭头函数,this
引用未指向您的类实例:
success: function (ExpressLibrary) { ... }
//change to
success: (ExpressLibrary) => { ... }
// or, quite funny you mentioned it
success: function (ExpressLibrary) { ... }.bind(this)
关于javascript - 更新 Angular2 中的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41927208/