Answer the question
In order to leave comments, you need to log in
How to pass variable value from component to Angular2 component?
Such a case: There is a menu component
@Component({
selector: 'menu',
template: '<div [ngClass]="{open : menu_stat}">menu elements...</div>',
})
export class MenuComponent {
menu_stat : boolean = false;
}
@Component({
selector: 'login',
template: `<button (click)="toggleMenu()">open<button>`,
})
export class LoginComponent {
menu:boolean = false;
toggleMenu(){
this.menu = !this.menu;
}
}
Answer the question
In order to leave comments, you need to log in
From an architectural point of view, it would be correct to create a service with an event. This service is declared in the system and both components receive it in the constructor using dependency injection. After that, it LoginComponent
will be able to trigger an event, and MenuComponent
- subscribe to it and process changes.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question