簡體   English   中英

更新Angular2中的視圖

[英]Update the view in Angular2

角度2新,面臨問題。 有一個API在我的項目中加載一個js文件。 問題是,我必須使用這個js文件而不是我的控制來改變它,基本上,這個js文件有很少的方法通過AJAX調用API並返回一個數組,但它返回它的方式很舊。 我需要的只是在數據進入后立即更新視圖,嘗試了很多方法,但仍然無法使其工作。 任何想法如何綁定和更新視圖?

 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)

我發現了這個問題,似乎就是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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM