3

Trying to get my head around @Input and @Output and was hoping for an indications as to how my approach is wrong.

In the app.component PARENT COMPONENT I have the following

@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);
}

this is the html

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

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

CHILD COMPONENT In the child(ExpensesComponent ) component is as follow

 @Input() dateChanged: any;

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

onDateSelected doesn't fire of What would be a good way of passing this date value to the child?

Thanks

1
  • 1
    It's not clear from your code what is parent and what is child. To me it looks like <app-expense> might be child and <app-component> might be parent but then your question doesn't make sense. If the child emits the event why would you want onDateSelected be called on the child when the event is received. Commented Dec 21, 2016 at 8:48

1 Answer 1

1

@Output() events are for parents to listen for events of child components, not the other way around.

For parent to child communication you can use data binding.

<app-expenses [selectedDateInChild]="selectedDateOfParent"></app-expenses>
Sign up to request clarification or add additional context in comments.

2 Comments

Cool thanks. is it possible to call a function in the child component from the parent that passes the date as a value?
You can make selectedDateInChild a setter then the code in the setter gets executed every time selectedDateOfParent changes

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.