使用@Input和@Output的自定义事件

时间:2016-12-21 08:46:41

标签: angular events

试图了解@Input@Output并希望了解我的方法是错误的。

app.component PARENT COMPONENT中,我有以下

@Output() dateChanged = new EventEmitter();
dateSelected:any;

onDateSelected() {
   this.dateSelected = this.yearValue + "|" + this.monthValue;
   console.log(this.dateSelected);//is selected and prints out to console
   this.dateChanged.emit(this.dateSelected);
}

这是html

<div>
 <!--app.component.html-->

      <app-expenses (dateChanged)="onDateSelected($event)"></app-expenses>
</div> 

儿童成分 在子(ExpensesComponent)组件中如下

 @Input() dateChanged: any;

 onDateSelected(e) {
   console.log(e);//doesnt reach this point
 }

onDateSelected不会触发 将这个日期值传递给孩子的好方法是什么?

由于

1 个答案:

答案 0 :(得分:1)

@Output()个事件是让父母听取儿童成分的事件,而不是相反。

对于父母与子女之间的沟通,您可以使用数据绑定。

<app-expenses [selectedDateInChild]="selectedDateOfParent"></app-expenses>
相关问题