angular2 在屏幕上只显示为对象对象
angular2 It appears only as an object object on the screen
在视图屏幕上,您无法获得所需的 json 键值。
我尝试了以下。 board.component.html
board.component.html
{{board.title}} > [对象对象],
{{board?.title}} > 没有,
{{棋盘 | Json}} > [{ "title" : "AAAAAA", "cont" : "AAAA1234", "date" : "2017-03-03", "hit":“5”}]
board.component.ts
@Component({
selector : 'board',
templateUrl : './board.component.html'
})
export class boardComponent implements OnInit{
pageTitle : string = 'board';
errorMessage : string;
board : Board[];
constructor(private _boardService : BoardService){}
ngOnInit() : void {
this._boardService.getBoard()
.subscribe(
boards => this.board = boards,
error => this.errorMessage = <any> error
);
console.log('board In OnInit');
}
}
board.service.ts
@Injectable()
export class BoardService{
private _boardUrl = '/src/api/board/board.json';
constructor(private _http : Http){}
getBoard() : Observable<Board[]>{
return this._http.get(this._boardUrl)
.map((response : Response) => <Board[]> response.json())
.do(data => console.log('Board : ' + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(error : Response){
return Observable.throw(error.json().error || 'Server error');
}
}
你应该使用 *ngFor 因为 board 是一个数组。或者如果你想访问第一个元素,你可以这样做,
{{board[0]?.title}}
在视图屏幕上,您无法获得所需的 json 键值。 我尝试了以下。 board.component.html
board.component.html
{{board.title}} > [对象对象],
{{board?.title}} > 没有,
{{棋盘 | Json}} > [{ "title" : "AAAAAA", "cont" : "AAAA1234", "date" : "2017-03-03", "hit":“5”}]
board.component.ts
@Component({
selector : 'board',
templateUrl : './board.component.html'
})
export class boardComponent implements OnInit{
pageTitle : string = 'board';
errorMessage : string;
board : Board[];
constructor(private _boardService : BoardService){}
ngOnInit() : void {
this._boardService.getBoard()
.subscribe(
boards => this.board = boards,
error => this.errorMessage = <any> error
);
console.log('board In OnInit');
}
}
board.service.ts
@Injectable()
export class BoardService{
private _boardUrl = '/src/api/board/board.json';
constructor(private _http : Http){}
getBoard() : Observable<Board[]>{
return this._http.get(this._boardUrl)
.map((response : Response) => <Board[]> response.json())
.do(data => console.log('Board : ' + JSON.stringify(data)))
.catch(this.handleError);
}
private handleError(error : Response){
return Observable.throw(error.json().error || 'Server error');
}
}
你应该使用 *ngFor 因为 board 是一个数组。或者如果你想访问第一个元素,你可以这样做,
{{board[0]?.title}}