I am currently working on a project that involves loading and selecting clients (not users, but more like customers).
However, I have encountered an issue where I am unable to subscribe to the Observables being loaded in my component. Despite trying various solutions found online, my lack of experience with Angular seems to be hindering me from finding the right solution.
What is functioning correctly at the moment:
- Loading clients in a select box
- Assigning the client's ID value to the options in the select box
- Sending the client's ID to client.service and saving the selected client as an Observable
My only challenge lies in the fact that the component fails to recognize changes in the Observables within the client.service.
Below is the code snippet for my client.service.ts:
import { Injectable } from '@angular/core';
import { Observable } from 'rxjs/Observable';
import { ApiService } from '../api.service';
import { Client } from './client';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
@Injectable()
export class ClientService {
clients$: Observable<Client[]>;
client: Client;
selectedClient$: Observable<Client[]>;
constructor(private api: ApiService) {
this.clients$ = this.getAll();
}
public getAll(): Observable<Client[]> {
return this.api.get<Client[]>('clients');
}
public setSelectedClient(clientId: number) {
this.clients$ = this.getAll();
if (clientId == null) {
// Do nothing
} else {
this.selectedClient$ = this.clients$.map(arr =>
{ return arr.find(client => client.id === clientId) });
}
}
public getSelectedClient() : Observable<Client> {
return this.selectedClient$;
}
}
This is the section of my component showing attempts made to resolve the issue:
import { Component, OnInit } from '@angular/core';
import { Observable } from "rxjs/Observable";
import { ClientService } from "../client/client.service";
import { Client } from "../client/client";
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/filter';
@Component({
selector: 'app-left-menu',
templateUrl: './left-menu.component.html',
styleUrls: ['./left-menu.component.css']
})
export class LeftMenuComponent implements OnInit {
selectedClient$: Observable<Client>;
client: Client = new Client();
clients$: Observable<Client[]>;
constructor(private clientService: ClientService) {}
ngOnInit() {
this.selectedClient$ = this.clientService.getSelectedClient();
this.clients$ = this.clientService.getAll();
}
public setSelectedClient(clientId: number) {
this.clientService.setSelectedClient(clientId);
}
}
Lastly, included below is the HTML segment used to display and select a client:
<select #selectClient [ngModel]="selectedClient$ | async"
(ngModelChange)="setSelectedClient(selectClient.value)">
<option *ngFor="let client of clients$ | async" [value]="client.id">
{{ client.firstName }}
{{ client.preposition }}
{{ client.lastName }}
</option>
</select>
<!-- Displaying selected client -->
<h2 *ngIf="selectedClient$">{{(selectedClient$ | async)?.firstName}}
</h2>
Your assistance in resolving this issue would be greatly appreciated. Thank you!