我從組件傳遞數據到另一個組件時缺少一些東西。我使用@Input傳遞數據,這是從http.get請求獲得的。問題是,當請求尚未解析時,我試圖從傳入的輸入中訪問屬性時出現錯誤。Angular2 @Input和http.get
//news.ts
import {Component} from 'angular2/core';
import {Http, HTTP_PROVIDERS} from 'angular2/http';
import {Pagination} from './pagination';
@Component({
selector: 'news',
templateUrl: 'app/news.html',
viewProviders: [HTTP_PROVIDERS],
directives: [Pagination]
})
export class News {
news = [];
pagination: Pagination;
constructor(http: Http) {
http.get('http://test.com/data')
.map(res => res.json())
.subscribe(news => this.news = news);
}
}
//pagination.ts
import {Component, Input} from 'angular2/core';
@Component({
selector: 'pagination',
templateUrl: 'app/pagination.html'
})
export class Pagination {
// page: int = 1;
@Input() config;
constructor() {
// this.page = this.config.number;
}
}
//Pagination.html
Page {{config.total}}
config.total在加載時產生錯誤。但是儘管{{config}}似乎能夠工作。
任何想法?
感謝
注意'async'管不能用於對象:http://stackoverflow.com/a/34561532/215945 –
非常感謝,第一個解決方案適合現在的需要!我找不到任何這個問題instellement ... – Choppy
好...我的標記答案正確:) –