【发布时间】:2022-02-02 16:57:54
【问题描述】:
有没有办法从字符串中组合打字稿中对象的键值?这是一个例子:
object[index].key=value
应该是,
object[index].["String".array[i]]=value
以下不起作用:
{{ object[index].{{"string".concat(Variable[index])}} }} <-- the rendering is perfectly fine but the execution is missing
我正在尝试在两个“ngFor”循环中读取一个对象,第一个循环充当数组的计数器,该数组的元素充当第二个循环中对象的键。
谢谢
编辑:
tage=['Montag','Dienstag','Mittwoch']
objectArray: {
id:Number;
name:String;
Montag:String;
Dienstag:String;
Mittwoch:String;
}[]=[
{id:0,name:'Klaus',Montag:'arbeiten',Dienstag:'rumgammeln',Mittwoch:'Frei'},
{id:1,name:'Dieter',Montag:'frei',Dienstag:'arbeiten',Mittwoch:'rumgammeln'},
{id:2,name:'Peter',Montag:'rumgammeln',Dienstag:'frei',Mittwoch:'arbeiten'},
]
模板
1:{{objectArray[0].Montag}}<br>
2:{{objectArray[0]['Montag']}}<br>
<br>
<ng-container *ngFor="let tag of tage; let i=index">
{{i}}:{{objectArray[0][tage[i]]}}<br> <------- NOT WORKING
</ng-container>
错误信息
(property) AppComponent.tage: string[]
Element implicitly has an 'any' type because expression of type 'string' can't be used to index type '{ id: Number; name: String; Montag: String; Dienstag: String; Mittwoch: String; }'.
No index signature with a parameter of type 'string' was found on type '{ id: Number; name: String; Montag: String; Dienstag: String; Mittwoch: String; }'.ngtsc(7053)
app.component.ts(8, 53): Error occurs in the template of component AppComponent.
【问题讨论】:
标签: arrays angular string object templates