我可以從模板中處理JSON,類似於我放置的第二個代碼?AngularJS2模板句柄JSON
第一代碼。
這種運作良好當它被用來.json
[]和使用.map()
@Component({
..//
template: `..//
<li *ngFor="#user of users">
JSON: {{ user.name }}
..//`
get(){
this.http.get('lib/sampleResAPI.json')
.map(res => res.json())
.subscribe(users => this.users = users);
..//
}
sampleResAPI.json(使用[] [])
[
{
"id": 362,
"name": "test",
..//
}
]
但我收到的答案不是[.json
]中的[]。警報顯示,罰款與users.name,alert(this.users.name);
但我無法處理它從模板{{users.name}}獲取未定義的屬性。
- >第二碼未發現
@Component({
..//
template: `..//
JSON: {{ users.name }}
..//`
get(){
this.http.get('lib/sampleResAPI.json')
//.map(res => res.json())
.subscribe(users => this.users = users.json());
//this.u = JSON.parse(this.users);
alert(this.users.name);
..//
}
sampleResAPI.json不[]
{
"id": 362,
"name": "test",
..//
}
有辦法處理.json
,類似於警報user.name
,在模板中,或者我做錯了什麼,就是這樣。
哦,我的好人,我已經嘗試了數百個沒有任何結果的語法,您的答案很好,謝謝 –
這是貓王操作員或安全導航操作員。只有當'user'爲!= null/undefined時,它纔會評估'.name'。你不能在任何地方使用它(例如'user?[x]'不支持。爲此,你需要'user?user [x]:null' –
非常感謝解釋 –