【问题标题】:Display attribute from child component to parent in angular 5以角度 5 显示从子组件到父组件的属性
【发布时间】:2018-06-21 15:21:12
【问题描述】:

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

<parent-component>

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

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

<parent-component>

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

【问题讨论】:

  • 查看输入和输出。它们非常方便

标签: angular5


【解决方案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>

【讨论】:

  • 我自己发现的,这正是您刚刚发布的内容。所以我会接受这个答案,所以它可以帮助其他人。谢谢。
猜你喜欢
  • 2018-12-08
  • 2019-05-05
  • 1970-01-01
  • 2023-04-09
  • 2020-08-24
  • 2018-10-09
  • 2021-03-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多