javascript - 更新 Angular2 中的 View

标签 javascript jquery ajax angular

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/

相关文章:

javascript - PHP函数无法调用

javascript - HTML5 中的 Shared Worker 和 Worker 有什么区别?

javascript - 将数据从 Cloudant/CouchDB 导出到 CSV

javascript - 在没有定义高度的情况下垂直和水平居中 div

javascript - 简单的 Javascript 图片库 - 带有分页点和多个实例

php - 如何将 json 编码的 PHP 数组转换为 Javascript 中的数组?

javascript - 在 Django 中处理模式窗口

Javascript 计数器在按键输入而不是按钮上?

javascript - jquery .html() append 后绑定(bind)不起作用

php - 为什么 jQuery 使用 UTF-16 进行 POSTing?