Skip to content
Advertisement

How to set value to parent component property when a button is clicked in Child component in Angular

I am new to Angular 7 (2+) & trying my hands on @Input & @Output. However, passing data from Parent to Child component via @Input is understood & in place.

However, very basic on the other hand passing data from Child to Parent component via using @Output concept is understood & but the implementation is not getting right.

Here is what I am trying. When a button is clicked in the Child component, a property in the parent component should be converted to Upper case & displayed.

ChildComponent.ts

JavaScript

ChildComponent.html

JavaScript

ParentComponent.ts

JavaScript

ParentComponent.html

JavaScript

No error in console or binding

From the above snippet, when the button in Child Component is clicked the parent Component’s property CustomerName should get the value & displayed?

Example: https://stackblitz.com/edit/angular-3vgorr

Advertisement

Answer

You are emitting event from app-child component so attach the handler for app-child component to make it work.

JavaScript

And within the ts file update value of cutomerName property.

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement