如何将值分配给角度7中的变量

问题描述 投票:0回答:2

我正在尝试将值分配给变量,但无法正常工作。如果有人知道,请帮助找到解决方法。

我的代码:

app.component.ts:

public power:any;
public ice:any;
public cake:any;

changeValue(prop,val){

this.[prop]=val;
console.log(this.[prop]);

}

this.changeValue("power","124525");
this.changeValue("ice","125658");
this.changeValue("cake","122568");
javascript typescript angular7
2个回答
0
投票

您正在函数中发送字符串。尝试使用此代码

public power:any;
public ice:any;
public cake:any;

changeValue(prop,val){
 console.log(prop);
 prop=val;
}

this.changeValue(this.power,"124525");
this.changeValue(this.ice,"125658");
this.changeValue(this.cake,"122568");

0
投票
public power:any;
public ice:any;
public cake:any;

changeValue(prop,val){

this[prop]=val;
console.log(this[prop]);

}

this.changeValue("power","124525");
this.changeValue("ice","125658");
this.changeValue("cake","122568");
© www.soinside.com 2019 - 2024. All rights reserved.