【发布时间】:2018-01-22 00:33:54
【问题描述】:
我有一个父组件 app-parent 和 app-child。 在 app-parent 我变成了来自我的 DataService 的数据
@Component({
selector: 'app-parent',
providers: [DataService]
})
export class Part1Component implements OnInit {
public title: string;
constructor(public dataService: DataService) {}
ngOnInit() {
this.dataService.get().subscribe(data => {
this.itemsSource = this.dataService.convert(data, 1);
this.title = this.itemsSource[0];
});
return this.title;
}
模板
<div id="content">Hallo!</div>
<app-child [title]='title'></app-child>
我想将标题传递给子组件。 所以在我的子组件中是:
@Injectable()
export class InfoComponent implements OnInit, OnDestroy {
@Input() title: any;
ngOnInit() {
console.log('Show me title')
console.log(this.title);
}
所以,我尝试使用@Input() 来传递我的数据,但它不起作用(如果标题我变成未定义的)。我想,这是因为在我的父组件中,我首先成为 DataService 的数据(因为如果我在 app-parent 中将标题设置为一开始的值,例如
public title = 'Hola!';
它有效..)
您能告诉我如何解决这个问题吗?
【问题讨论】:
标签: javascript angular typescript