如何使用angular打印以特殊字符开头的键的JSON值?
当我尝试使用@place打印时,当其他键可以访问时,它会抛出错误 [{ “ID”:“001”,如何使用angular打印以特殊字符开头的键的JSON值?,json,angular,typescript,Json,Angular,Typescript,当我尝试使用@place打印时,当其他键可以访问时,它会抛出错误 [{ “ID”:“001”, “名称”:“欧亚领鸽”, “类型”:“鸽子”, “学名”:“Streptopelia”, “@place”:“纽约” }] 这就是我得到的错误: 分析器错误:意外的标记词法器错误:表达式[bird@place]第6列出现意外字符[@],在[{bird@place}]第7列出现预期的标识符或关键字 组件.ts文件如下: import { Component } from '@angular/core';
“名称”:“欧亚领鸽”,
“类型”:“鸽子”,
“学名”:“Streptopelia”,
“@place”:“纽约” }] 这就是我得到的错误:
分析器错误:意外的标记词法器错误:表达式[bird@place]第6列出现意外字符[@],在[{bird@place}]第7列出现预期的标识符或关键字 组件.ts文件如下:
import { Component } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { HttpErrorResponse } from '@angular/common/http';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
title = 'JSON to Table ';
constructor (private httpService: HttpClient) { }
arrBirds: string [];
ngOnInit () {
this.httpService.get('./assets/birds.json').subscribe(
data => {
this.arrBirds = data as string []; // FILL THE ARRAY WITH DATA.
// this.arrService = JSON.stringify(data);
console.log(this.arrBirds);
},
(err: HttpErrorResponse) => {
console.log (err.message);
}
);
}
}
component.html文件如下所示:
<table *ngIf="arrBirds">
<tr>
<th>ID</th>
<th>Name of Bird</th>
<th>Type of Bird</th>
<th>Place</th>
</tr>
<tr *ngFor="let bird of arrBirds">
<td>{{bird.ID}}</td>
<td>{{bird.Name}}</td>
<td>{{bird.Type}}</td>
<td>{{bird.@place}}</td>
</tr>
</table>
身份证件
鸟名
鸟种
放置
{{bird.ID}
{{bird.Name}
{{bird.Type}
{{鸟。@place}
在HTML中,我不知道还有什么方法可以调用属性。我能看到的唯一解决办法是做一个getter
在TS中
在HTML中
{{ place }}
正如我在评论中提到的,可以使用括号符号而不是点符号
{{bird['ID']}
{{bird['Name']}
{{bird['Type']}
{{bird['@place']}
工作示例:您确定大写字母p(而不是小p)没有打字错误吗?您可以尝试用括号表示法
bird['@Place']
代替点表示法。@guilhermes这是个错误。但在使用p而不是p之后,我仍然会得到相同的错误。@MichaelD ui显示bird['@place'],而不是打印值。@Soubhagyashoo:请显示您试图访问该属性的方式和位置。
{{ place }}