好的,所以我一直在爲此掙扎了一段時間。我使用名爲「Contenful」的服務來充當CMS,並使用它們的API來獲取JSON數據。Angular 2無法從模板訪問HTTP獲得響應
我有一個叫做「editor.service.ts」服務如下(我已經隱藏了「SpaceID」和「訪問令牌」隱私):
import { Injectable } from '@angular/core';
import { Http, Response } from '@angular/http';
import { Observable } from 'rxjs/Observable';
import 'rxjs/add/operator/map';
@Injectable()
export class EditorService {
private baseURL = 'https://cdn.contentful.com';
private spaceID = 'HIDDEN';
private accessToken ='HIDDEN';
private blocksURL = this.baseURL + '/spaces/' + this.spaceID + '/entries?access_token=' + this.accessToken + '&content_type=blocks';
constructor(private http: Http) {}
getBlocks(): Observable<any> {
return this.http.get(this.blocksURL).map((res: Response) => res);
}
}
然後在我的組件調用'editor.component.ts' 我有這樣的:
import { Component, OnInit } from '@angular/core';
import { EditorService } from './../editor.service';
@Component({
selector: 'app-editor',
templateUrl: './editor.component.html',
styleUrls: ['./editor.component.css'],
providers: [EditorService]
})
export class EditorComponent implements OnInit {
blocksJSON: any;
constructor(private editorService: EditorService) {}
ngOnInit() {
this.editorService.getBlocks().subscribe(res => {
this.blocksJSON = res.json().items;
console.log(this.blocksJSON); //THIS WORKS
});
}
}
這裏有一個是越來越我困惑的一點。該文件中的console.log()行正好打印出我想要的內容,所以我知道該API工作正常。令人困惑的是現在。
在我的模板文件,我有這樣一行:
<h1 *ngFor="let block of blocksJSON; let i = index">{{block[i].fields.blockName}}</h1>
在我看來,這應該只是工作。不過,我不斷收到錯誤
ERROR TypeError: Cannot read property 'fields' of undefined
它打印出此錯誤3次,即使在目前,有在數組「塊」只有1對象。但我不認爲有1個對象是一個問題...(我可能是錯的,雖然)
我不知道這是爲什麼,因爲console.log打印出我想要的數據,所以我知道它應該被定義。我唯一的想法是角度2 HTTP get是異步的,但我不知道這是否是問題。
任何意見將不勝感激。謝謝。
[angular2的可能重複:錯誤:類型錯誤:無法讀取undefi的屬性'...' ned](https://stackoverflow.com/questions/41242793/angular2-error-typeerror-cannot-read-property-of-undefined) – Alex
和更多的建議... https://stackoverflow.com/questions/34734671/ observable-type-error-can-read-property-of-undefined大量的問答環節;) – Alex
@ AJT_82感謝您的回答並抱歉發佈重複。根據2個答案,我認爲我需要將h1包裝在* ngIf中。我這樣做如下:但我仍然得到相同的錯誤。你有什麼想法,或者我應該繼續尋找:) –