我有一个父组件,我想在其中显示一个 Headers ,该 Headers 是包含在子组件上显示的对象上的属性 . 我会是这样的:
<parent-component> <h1>{{title_i_want_to_display}}</h1> <child-component><p>{{object.title}}</p></child-component> <parent-component>
如何捕获该object.title并将其显示在父项的h1标记内?
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>
1 回答
child.component.ts
parent.component.ts
parent.component.html