I'm trying to set a value in one component, if a function is triggered in another component.
However, I can not get it to work.
I am using the approach given in this post
I would like my Sidebar to only show up after the Login-Button was pressed.
I wrote a service component like this:
component-interaction.service.ts
import { EventEmitter, Output } from '@angular/core';
import { Injectable } from '@angular/core';
@Injectable()
export class ComponentInteractionService {
showSidebarEmitter: EventEmitter<any> = new EventEmitter();
setSidebar(showSidebar:boolean){
this.showSidebarEmitter.emit(showSidebar);
}
}
and then I subscribe the components that would recieve the new values like this in my sidebar.component.ts:
import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { ComponentInteractionService } from '../services/component-interaction.service';
@Component({
moduleId: module.id,
selector: 'app-sidebar',
templateUrl: 'sidebar.component.html',
styleUrls: ['sidebar.component.css'],
providers: [ ComponentInteractionService ]
})
export class SidebarComponent implements OnInit {
showSidebar: boolean = false;
constructor(private _router: Router, private _componentInteractionService: ComponentInteractionService) { }
ngOnInit() {
this._componentInteractionService.showSidebarEmitter.subscribe(res =>this.showSidebar = res);
}
}
and I'm setting the value in my login.component.ts like this:
import { Component, OnInit, EventEmitter, Output } from '@angular/core';
import { Observable } from 'rxjs/Rx';
import { ComponentInteractionService } from '../services/component-interaction.service';
@Component({
moduleId: module.id,
selector: 'app-login',
templateUrl: 'login.component.html',
styleUrls: ['login.component.css'],
providers: [ ComponentInteractionService ]
})
export class LoginComponent implements OnInit {
login(){
this._componentInteractionService.setSidebar(true);
}
}
now, since the value in my sidebar.component.ts is subscribed to the emit event it should be updated as soon as it is changed, should it not?
Thank you for your help :)