我从组件传递数据到另一个组件时缺少一些东西。我使用@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
好...我的标记答案正确:) –