首页 文章

在角度5中显示从子组件到父组件的属性

提问于
浏览
0

我有一个父组件,我想在其中显示一个 Headers ,该 Headers 是包含在子组件上显示的对象上的属性 . 我会是这样的:

<parent-component>

<h1>{{title_i_want_to_display}}</h1>

<child-component><p>{{object.title}}</p></child-component>

<parent-component>

如何捕获该object.title并将其显示在父项的h1标记内?

1 回答

  • 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>
    

相关问题