user68288
user68288

Reputation: 774

Need help accessing variable from Service in Angular 2

Passing the URL id from the last page a user was on to a service that I can reference in a dialog.

issuer.service.ts

import { Injectable, EventEmitter } from '@angular/core';
import { Observable, of } from 'rxjs';
import { BehaviorSubject } from 'rxjs';

@Injectable()
export class IssuerService {


private urlidSource = new BehaviorSubject<string>('');
currentUrlid = this.urlidSource.asObservable();
public onChange: EventEmitter<string> = new EventEmitter<string>();

  constructor () {
  }

  changeUrlid(urlid: string) {
    this.currentUrlid = of(urlid);
    this.onChange.emit(urlid);
  }

  getUrlid(currentUrlid: string) {
    return this.currentUrlid;
  }


}

Page that has the URL id I want (dashboard.component.ts)

import { IssuerService } from './../../issuer.service';
import { ActivatedRoute } from '@angular/router';
import { Router } from '@angular/router';
urlid: string;
  constructor(
    private route: ActivatedRoute,
    private router: Router,
    private issuerService: IssuerService,
    public dialog: MatDialog
  ) {}

newUrlid() {
  this.issuerService.changeUrlid(this.route.snapshot.paramMap.get('id'));
  console.log(this.urlid);
}

  ngOnInit() {
    // Get URL ID
    this.issuerService.onChange.subscribe(urlid => this.urlid = urlid);
    this.newUrlid();
}

Component I want to read the value in:

    import { ActivatedRoute } from '@angular/router';
    import { Router } from '@angular/router';
    import { IssuerService } from './../../issuer.service';

        urlid: string;

        constructor(
          private route: ActivatedRoute,
          private router: Router,
          private issuerService: IssuerService,
          public dialog: MatDialog
        ) {}

  ngOnInit() {
 this.issuerService.onChange.subscribe(urlid => {
           this.urlid = urlid;
           console.log(this.urlid);
   });

  }

So currently when I visit my dashboard page it will display the value of 2 which is correct. My goal is that when a user visits any page I can read this value of 2. How can I access this value? The above code works and my Header displays 2 but only when on the dashboard page. I need it to display 2 no matter what page the user is on.

Upvotes: 0

Views: 41

Answers (2)

Chunbin Li
Chunbin Li

Reputation: 2244

you can see this example, and It's modified list:

  • use queryPamas to get query string, not params (DashboardComponent)

  • use ReplaySubject(1) to return the last urlId; it's don't have a default value, just return prev one value (IssuerService)

  • get observable from getUrlid and subscribe it in components that want to show url id

enter image description here

export class IssuerService {


  private urlidSource = new ReplaySubject<string>(1);

  constructor() {
  }

  changeUrlid(urlid: string) {
    this.urlidSource.next(urlid);
  }

  getUrlid() {
    return this.urlidSource;
  }

}

export class DashboardComponent implements OnInit {

  urlid: string;
  constructor(
    // private route: ActivatedRoute,
    private router: Router,
    private issuerService: IssuerService,
    // public dialog: MatDialog
  ) { }

  newUrlid() {
    // Get URL ID
    this.route.queryParams.subscribe((queryParam) => {
      const id = queryParam['id'];
      if (!id) {
        return;
      }
      this.issuerService.changeUrlid(id);
    });
  }

  ngOnInit() {
    this.newUrlid();

    this.issuerService.getUrlid().subscribe(urlid => {
      this.urlid = urlid;
    });
  }

}

export class HelloComponent implements OnInit {
  urlid;
  constructor(
    private issuerService: IssuerService
  ) { }

  ngOnInit() {
    this.issuerService.getUrlid().subscribe(urlid => {
      this.urlid = urlid;
    });
  }
}

Upvotes: 1

Sajeetharan
Sajeetharan

Reputation: 222522

You do not need a parameter for your get Method since you already have the value inside the service,

  getUrlid() {
    return this.currentUrlid;
  }

and you can use retrieve the value in the 2nd component as follows,

this.issuerService.currentUrlid.subscribe((value: string) => {
         this.urlid = value;
}

Upvotes: 1

Related Questions