G
G
green1762016-08-29 11:39:56
Angular
green176, 2016-08-29 11:39:56

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

there is a login panel component
@Component({
    selector: 'login',
    template: `<button (click)="toggleMenu()">open<button>`,
})
export class LoginComponent {
   menu:boolean = false;
   toggleMenu(){
      this.menu = !this.menu;
   }
}

It is necessary that when I click on the button in the login panel, I would display a menu, i.e. so that the menu_stat variable in the menu component becomes true
If possible, then on the fingers, because I rummaged through the docks, and I didn’t really find a normal solution

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Andrew, 2016-08-29
@impwx

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 LoginComponentwill be able to trigger an event, and MenuComponent- subscribe to it and process changes.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question