PDA

View Full Version : Angular - pass value to a service and get in constructor of another component



jarvboy
Feb 20th, 2019, 05:01 PM
Hi,

so, this is the if statement I would like to be run in the constructor of my pubs.component


if (this.townService.selectedLetter === '') {
this.townService.getTowns().subscribe(result => this.townsList = result);
} else {
this.townService.getTownsLetter().subscribe(result => this.townsList = result);
}

this.townService.selectedLetter is undefined

I am passing it through in this initial function in my first-letter.component


myTown($event) {
this.townservice.selectedLetter = $event;
this.pubscomp.letterClicked($event);
}



and this is my service


selectedLetter: any;
private _townfirstletter = 'http://www.mypubspace.com/pubsmobile/wherefirstletter.php?firstletter={{selectedLetter}}';
constructor(private http: HttpClient) { }

getTownsLetter(): Observable<object> {
return this.http.get(this._townfirstletter);
}


Please help?

jarvboy
Feb 21st, 2019, 11:57 AM
Can anyone help here please?