gpt4 book ai didi

angular - 范围变量 - 为变量赋值时出错

转载 作者:太空狗 更新时间:2023-10-29 18:20:05 25 4
gpt4 key购买 nike

如何从函数的回调中访问变量。我将在问题中更直接地展示示例

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

最佳答案

不要在 TypeScript 中使用 function。将它们替换为 ()=>{} 语句。

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

当您使用 function() {} 时,this 不是持久性的,因为 ()=>{} 保留 这个引用。

您的替代方法是在函数上使用 bind(this)

关于angular - 范围变量 - 为变量赋值时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44961418/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com