1

I'm trying to create a variable in my service, that would be accessable whenever I inject this service. However, I cannot populate my login$ of BehaviorSubject type. I checked this and this, but I'm having difficulties understanding how it's working.

Component

this.loginService.login = user.value.username; // OK
this.loginService.pass = user.value.password; // OK
this.loginService.emitLogin(user.value.username); // UNDEFINED - user.value.username is input field of form group

LoginService

export class LoginService {

  public login: string;
  public pass: string;
  public login$ = new BehaviorSubject(this.login);

  emitLogin(value: string) {
    this.login$.next(value);
  }

  constructor(private http: Http) { } 
}

Also, I'd like to use login$ in my DriverService. Here's what have I done till now:

DriverService

    export class DriverService {    
      private login;

      constructor(private http: Http, private loginService: LoginService) {
        this.loginService.login$.subscribe(value => {
          console.log(value);
          this.login = value;
        });
      }
}

How can I populate and properly, and make it usable in other services?

Thanks

Haseoh
  • 910
  • 3
  • 18
  • 38
  • 1
    `this.login` is `undefined` initially... :) Try `public login: string = ''` in service. – AT82 Jun 09 '17 at 12:31

1 Answers1

1

BehviorSubject always initialized with some value but in your code your initializing with variable definition.

 public login: string;
 public login$ = new BehaviorSubject(this.login);

do something like this

public login$ = new BehaviorSubject('');
emitLogin(value: string) {
    this.login$.next(value);
  }
CharanRoot
  • 6,181
  • 2
  • 27
  • 45
  • I applied your changes. Right now it's something like this in console: `BehaviorSubject... _value:"Testing"...` and in `DriverService` there's empty string. Any further ideas? – Haseoh Jun 09 '17 at 12:55
  • as per my understating Login and DriverService are initializing at same time time so BehaviorSubject have initial value of ' ' (empty string). In drivieService first value will be ' ' because BehaviorSubject first value is empty. once app initialization done with your this.loginService.emitLogin(user.value.username); will be called and correct will be pushed. Why are you using BehaviorSubject here i think in your case plain Subject enough ( https://stackoverflow.com/questions/21257830/how-can-publishsubject-and-behaviorsubject-be-unsubscribed-from) – CharanRoot Jun 09 '17 at 13:07
  • I think these services are not initializing at the same time, because when I initialize the app, 1st thing you have to do is go through login proccess, then you have access to `DriverService`. I use BehaviorSubject because I couldn't make standard Subject work (well, BehaviorSubject doesn't work either lol). – Haseoh Jun 09 '17 at 13:15
  • Initializing happen based on app module definition. try console log in constructor in both service. create plunker may be i can help – CharanRoot Jun 09 '17 at 13:27
  • Sorry, I'm late: here goes the plunker: https://plnkr.co/edit/X38rbSArps32AduSa4la?p=preview – Haseoh Jun 11 '17 at 11:57