Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

updating variable changes in components from a service with angular2

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) {
  }
}
like image 698
B Hull Avatar asked Jan 11 '16 05:01

B Hull


3 Answers

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

like image 193
Günter Zöchbauer Avatar answered Nov 08 '22 04:11

Günter Zöchbauer


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;
  }
}
like image 37
hansmaad Avatar answered Nov 08 '22 05:11

hansmaad


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

like image 12
Thierry Templier Avatar answered Nov 08 '22 04:11

Thierry Templier