如何对角度8中的对象设置为真

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

我正在尝试根据传递的值设置true或false,但不能在angular 8中使用。如果有人知道,请帮助解决这个问题。

app.component.ts:

this.main = { 
    power: false,
    electrical: false,
    wire: false,
    current: false
}

testing(flag){ 
    this.main[flag]=true;
}

testing(wire);
javascript angular7 angular8
2个回答
0
投票

假设您在组件类中,则在调用wire时未定义testing。它必须是一个字符串才能工作。另外,您使用的是this错误。试试:

main = { 
  power: false,
  electrical: false,
  wire: false,
  current: false
}

testing(flag){ 
  this.main[flag] = true;
}

然后在同一组件的另一个函数内的某个地方:

this.testing("wire");

0
投票
main={ 
power:false,
electrical:false,
wire:false,
current:false
}

testing(flag, value){ 
this.main[flag]=value;
}

this.testing('wire', flase);

请尝试这样

© www.soinside.com 2019 - 2024. All rights reserved.