0

I'm working on an Angular2 app , I'm loading a data from a service to my component as a subject

public  type1Choisi: any;

constructor(
  public formeService: FormeService, ...) {
    this.formeService._type1.subscribe(type1 => this.type1Choisi = type1);
}

and I'm using its value perfectly in my html view :

Type 1 selectionnè est :{{type1Choisi}}

but I'm not able to use it in my ts file of my component to do same processing :

if (this.type1Choisi ===102){  //Here type1Choisi doesn't give the value
  ParamService.dormant.Porte_ON = 1.00;
}

am declaring my data in the service as subject

public _type1 = new Subject();

Anybody may tell me how can i use my sybject not ontly in my view but also in my ts code ????

Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
firasKoubaa
  • 6,439
  • 25
  • 79
  • 148

1 Answers1

0

To get a value from a subject, you need to subscribe on it:

this._type1.subscribe(
  (event) => {
  }
);

When an event is received on it, the callback defined when subscribing will be called.

You can send an event this way:

this._type1.emit('some string event');
Thierry Templier
  • 198,364
  • 44
  • 396
  • 360
  • i have already done it , and i wanna load the value of the output variable : this.formeService._type1.subscribe(type1 => this.type1Choisi = type1); ========> type1CHoisi ???? – firasKoubaa Apr 04 '16 at 14:32
  • Where do you want to use this property? When the callback is triggered? – Thierry Templier Apr 04 '16 at 14:35
  • i wanna load the value of " type1Choisi " to use it in a further traitement on another variable , at least how can i display it value : (console.log(this.type1CHoisi) ) ??, – firasKoubaa Apr 04 '16 at 14:39
  • Only based on observables / subjects, it's not possible. They doesn't contain a state but "only" allow you to receive events and errors. You can't load something. That said you can cache it into your service (the last value) using the `do` operator. Here is a sample: http://stackoverflow.com/questions/35992877/angular-2-best-practice-to-load-data-from-a-server-one-time-and-share-results-t/35993192#35993192. – Thierry Templier Apr 04 '16 at 14:43