2017-07-07 7 views
3

関数のコールバック内から変数にアクセスする方法。質問にもっと直接的になる例を示します。スコープ変数 - 値を変数に代入中のエラー

export class FormComponent { 

    public pedidoSoft:PedidoSoft = new PedidoSoft(); 

    getBrandCard(){ 

     PagSeguroDirectPayment.getBrand({ 

      cardBin: this.pedidoSoft.numCard, 
      success: function(response) { 

        this.pedidoSoft.bandCard = response.brand.name; 

      }, 
      error: function(response) { }, 
      complete: function(response) { } 
     }); 

    } 

次のエラーが表示されます。 this.pedidoSoft.bandCardがresponse.brand.name

enter image description here

+0

とUbuntuを使用しているもの:) – cgTag

答えて

4

活字体でfunctionを使用しないでくださいの値を受信したときに、このエラーがあります。かわりに()=>{}文で置き換えてください。

 success: (response) => { 
       this.pedidoSoft.bandCard = response.brand.name; 
     }, 
     error: (response) => { }, 
     complete: (response) => { } 

あなたがfunction() {}thisを使用()=>{}this参照を保存する場所として、永続的ではありません。

代わりに、bind(this)を機能に使用することができます。

+1

あなたの回答が私の問題を解決しました。私は何時間もこれを理解しようとしてきました。ありがとうございました。 –

関連する問題