我正在尝试将值分配给变量,但无法正常工作。如果有人知道,请帮助找到解决方法。
我的代码:
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");
您正在函数中发送字符串。尝试使用此代码
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");
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");