1


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 :)

1 Answer 1

4

You have provide two instances of your service, ComponentInteractionService must be in only one provider (for exemple in AppComponent)

Sign up to request clarification or add additional context in comments.

6 Comments

If i'm unclear : remove ComponentInteractionService from your "providers" tag in component decorators, keep it just in constructor, then put ComponentInteractionService in the providers tag of AppComponent (or other name for the root component)
awesome, it's working now. Thank you for the quick answer!
I don't know if you intentionnaly want to use previous RC5 Angular2 version, but if you don't, you probably also should remove providers stuff from decorator and put it in a @module
so basically I put all the providers inside my app.module.ts file and remove them fromm all others? I started when it was still on RC4 but tried to update all to RC5
You can put a provider in any module, you juste have to take caution to two things : a service must be in only one module (shared module could be tricky) and lazy loading can create issues if the module is not loaded yet
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.