Skip to content
Advertisement

Click event on child Component Angular 13 to remove class in grand-parent component

Hello I have 3 components in Angular 13:

Child : Burger-menu

Parent : Header

Grand-Parent : app.component.html

I have a close burger menu button in the burger-menu component. when I click on it, I want to remove a class that is on the header. The header is located in the grandparent component. Here is my code :

Grand-parent component:

app.component.html

JavaScript

app.component.ts

JavaScript

Parent component :

header.component.html

JavaScript

header.component.ts

JavaScript

Child Component :

burger-menu.component.html

JavaScript

burger-menu.component.ts

JavaScript

When I click on the open menu button (in the parent component), I have the –openned class added to the header (grand parent component). Now where I have a problem is to remove this class when I click on the burger close button (child component)

Any tips to help me solve this problem?

Thank you in advance

Advertisement

Answer

You’re not taking advantage of the emitted value from header component. You’re emitting this.addClassEvent.emit('headerMain --openned') and then in the app component statically passing a string to the function (addClassEvent)="receiveClass('--openned')".

Change it to (addClassEvent)="receiveClass($event)" and emit an empty string on burger close in header component like this

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