我有一个父组件,我想在其中显示标题,该标题是包含在子组件上显示的对象上的属性。我会是这样的:
<parent-component>
<h1>{{title_i_want_to_display}}</h1>
<child-component><p>{{object.title}}</p></child-component>
<parent-component>
如何捕获该object.title并将其显示在父对象的h1标签内?
答案 0 :(得分:1)
child.component.ts
@Output titleEvent = new EventEmiter<string>();
ngOnInit(){
this.titleEvent.emit(object.title);
}
parent.component.ts
public myTitle: string;
onTitleFetch(title){
this.myTitle = title;
}
parent.component.html
<h1>{{myTitle}}</h1>
<child-component (titleEvent)="onTitleFetch($event)"></child-component>