笑故挽风 2017-01-29 23:37 采纳率: 100%
浏览 33

更新Angular2中的视图

new in angular 2 and facing a problem here. Have an API which loads a js file in my project. The problem is, I have to use this js file and is not in my control to change it, basically, this js file has few methods which call an API via AJAX and return an array, but the way it returns it quite old fashion. All I need is to update the view as soon the data comes in, tried many ways, but still cant get it worked. Any ideas how can I bind and update the 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);
            }
        });
    }

}

</div>
  • 写回答

2条回答 默认 最新

  • weixin_33716557 2017-01-29 23:53
    关注

    Looks like you need to use arrow function, this reference does not point to your class instance:

     success: function (ExpressLibrary) { ... }
     //change to
     success: (ExpressLibrary) => { ... }
     // or, quite funny you mentioned it
     success: function (ExpressLibrary) { ... }.bind(this)
    
    评论

报告相同问题?