81

My app has a NameService which holds the name.

There are two child components of App, Navbar and TheContent which reference this service. Whenever the name changes in the service, i want it to update in both of the other components. How can i do this?

import {Component, Injectable} from 'angular2/core'

// Name Service

@Injectable()
class NameService {
  name: any;
  constructor() {
    this.name = "Jack";
  }
  change(){
    this.name = "Jane";
  }
}

// The navbar
@Component({
  selector: 'navbar',
  template: '<div>This is the navbar, user name is {{name}}.</div>'
})
export class Navbar {
  name: any;
  constructor(nameService: NameService) {
    this.name = nameService.name;
  }
}

// The content area
@Component({
  selector: 'thecontent',
  template: '<div>This is the content area. Hello user {{name}}. <button (click)=changeMyName()>Change the name</button></div>'
})
export class TheContent {

  name: any;

  constructor(public nameService: NameService) {
    this.name = nameService.name;
  }
  changeMyName() {
       this.nameService.change();
     console.log(this.nameService.name);
  }
}


@Component({
  selector: 'app',
  providers: [NameService],
  directives: [TheContent, Navbar],
  template: '<navbar></navbar><thecontent></thecontent>'
})
export class App {
  constructor(public nameService: NameService) {
  }
}

3 Answers 3

113

Provide an event in the service and subscribe to it in the components:

@Injectable()
class NameService {
  name: any;
  // EventEmitter should not be used this way - only for `@Output()`s
  //nameChange: EventEmitter<string> = new EventEmitter<string>();
  nameChange: Subject<string> = new Subject<string>();
  constructor() {
    this.name = "Jack";
  }
  change(){
    this.name = 'Jane';
    this.nameChange.next(this.name);
  }
}
export class SomeComponent { 
  constructor(private nameService: NameService) {
    this.name = nameService.name;
    this._subscription = nameService.nameChange.subscribe((value) => { 
      this.name = value; 
    });
  }

  ngOnDestroy() {
   //prevent memory leak when component destroyed
    this._subscription.unsubscribe();
  }
}

See also
angular.io - COMPONENT INTERACTION - Parent and children communicate via a service

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

23 Comments

Also remember to unsubscribe from the event in the component else, the component instance will not be released. Need to call unsubscribe on the object returned by subscribe function.
let subscription=nameService.nameChange.subscribe((value) => { this.name = value; }); subscription.unsubscribe(). I have faced this issue hence the disclaimer. If subscription has to cancelled from other method, the variable should be class level
@micronyks The question was not about keeping the value when reloaded IMHO. For this you need to persist the data to a server and reload it or at least use localstorage, to store it in the browser.
I've been try to implement this solution, but I get an error in the service that says property 'emit' does not exist on type 'Subject<string>'. Am I missing something in the above solution? Is there a working Plunkr of this solution?
@Scipion In general, if you subscribe imperatively you should unsibscribe imperatively as well. In this case the component that subscribes to this service would probably still execute the callback passed to subscribe() on name change, even when it is already destroyed because the subscription stays active until the component gets garbage collected, which probably won't happen until the subscription is cancelled.
|
24

Since name in NameService is a primitive type, you'll get different instance in the service and your components. When you change name in NameService, the component properties still have the initial value and the binding doesn't work as expected.

You should apply the angular1 "dot rule" here and bind to a reference type. Change NameService to store an object that contains the name.

export interface Info {
   name:string;
}

@Injectable()
class NameService {
  info: Info = { name : "Jack" };
  change(){
    this.info.name = "Jane";
  }
}

You can bind to this object and get updates to the name property automatically.

// The navbar
@Component({
  selector: 'navbar',
  template: '<div>This is the navbar, user name is {{info.name}}.</div>'
})
export class Navbar {
  info: Info;
  constructor(nameService: NameService) {
    this.info = nameService.info;
  }
}

7 Comments

This is the way I would do it too. In angular 1 "always have a dot in your model" is a good way to reduce the effort to sync your bindings, it may be appropriate in Angular2 as well, depending on the use case. Without it, using event emitters to sync the model can get tedious.
wow, why does this work compared to the EventEmitter way? it feels like so much less work?
@micronyks In angular2 you can have multiple service instances. You should not define providers on both components. This way each component has its own NameService plnkr.co/edit/nScZgT1hsIKHZ7Z5E6u3?p=preview
@BHull, it works because with an object, both the service and the component refer to the same one object... and they each have a reference to that one object. With a primitive type (string, number, boolean), the component gets its own copy of the primitive value in the constructor. Any change you make to the primitive name property in the service is unrelated to any change you make to primitive name property in the component.
"If you change name in NameService you've got a different instance in the service and your components." Even if you don't change the value, the component gets its own name primitive property in the constructor. The initial value of this new primitive property is the value of the name property in the service, at the time of the assignment. The point I'm trying to make is that with primitive types, you never have the same "instance". One with reference types can you get two (or more) things referencing the same instance.
|
12

I think that the solution provided by Günter is the best one.

That said, you must be aware that Angular2 services are singleton that take place into a tree of injectors. This means that:

  • if you define your service at the application level (within the second parameter of the bootstrap method), the instance can be share by all elements (components and service).
  • if you define your service at the component level (within the providers attribute), the instance will be specific to the component and its sub components.

For more details of such aspect, you can have a look at the "Hierarchical Dependency Injection" doc: https://angular.io/docs/ts/latest/guide/hierarchical-dependency-injection.html

Hope it helps you, Thierry

Comments

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.