动态 select this.variable angular 12
dynamically select this.variable angular 12
我在选择组件中预定义的变量时遇到问题。
这些是变量:
public editable1: number = 0;
public editable2: number = 0;
public editable3: number = 0;
public editable4: number = 0;
public editable5: number = 0;
下一个数组数组:
public editors: []=[
['editable1', 'editable3', 'editable5'],
['editable1', 'editable2', 'editable3']
]
最终我想根据数组
中提供的列表增加变量
this.editors.forEach(element => {
element.forEach(e => {
this['e']++; // this suppose to be pointing to predefined variables, but it does not
});
});
我该如何解决?
编辑:提供的建议运行良好,但我想为更复杂的代码添加有关语法的附加信息。
我已经更改了变量,必须对其进行编辑,现在它位于 service.object.object.key:value
所以它看起来像这样:
this.editors.forEach(element => {
element.forEach(e => {
(this.service.object as any)[e].key++; // this way I increment value of object, that is inside object, which is in service
});
});
您可以将您的组件转换为任何组件,然后您可以使用 ['prop_name'] sytnax:
访问其属性
this.editors.forEach((element) => {
element.forEach((e) => {
(this as any)[e]++;
});
});
我在选择组件中预定义的变量时遇到问题。 这些是变量:
public editable1: number = 0;
public editable2: number = 0;
public editable3: number = 0;
public editable4: number = 0;
public editable5: number = 0;
下一个数组数组:
public editors: []=[
['editable1', 'editable3', 'editable5'],
['editable1', 'editable2', 'editable3']
]
最终我想根据数组
中提供的列表增加变量this.editors.forEach(element => {
element.forEach(e => {
this['e']++; // this suppose to be pointing to predefined variables, but it does not
});
});
我该如何解决?
编辑:提供的建议运行良好,但我想为更复杂的代码添加有关语法的附加信息。 我已经更改了变量,必须对其进行编辑,现在它位于 service.object.object.key:value 所以它看起来像这样:
this.editors.forEach(element => {
element.forEach(e => {
(this.service.object as any)[e].key++; // this way I increment value of object, that is inside object, which is in service
});
});
您可以将您的组件转换为任何组件,然后您可以使用 ['prop_name'] sytnax:
访问其属性this.editors.forEach((element) => {
element.forEach((e) => {
(this as any)[e]++;
});
});