【问题标题】:How to pass events from parent to child component?如何将事件从父组件传递给子组件?
【发布时间】:2018-07-24 17:00:15
【问题描述】:

如何将对象(包含事件)从父组件传递到子组件,以及如何将@Output 发送到该事件从子组件到父组件。

父组件

ts 文件:

Obj = { 'event': 'onSelect($event)'}; 
onSelect(val){console.log('from child to parent')}

html 文件:

<child-component [Obj]="Obj"></child-component>

子组件

如何发出/触发动作,这是从Obj 发送的。

【问题讨论】:

标签: javascript angular events output


【解决方案1】:

EventEmitter 就是你要找的东西

儿童 TS

@Output onSelect: EventEmitter<Object> = new EventEmitter();

select(){
  // myObject is object you want to send to parent
  onSelect.emit(myObject);
}

子 HTML

<a (click)="select()">Select this</a>

父 HTML

<child-block [onSelect]="onChildSelect($event)">

父 TS

//obj will contain object you've sent from your child 
onChildSelect(obj: Object){
   //your code
}

【讨论】:

    猜你喜欢
    • 2022-11-24
    • 1970-01-01
    • 1970-01-01
    • 2020-03-30
    • 2020-02-04
    • 2020-11-01
    • 2017-08-16
    • 2020-11-05
    • 2018-04-16
    相关资源
    最近更新 更多