card.service.ts
@Injectable({
providedIn: 'root'
})
export class CardService {
drugs: Drugs = [];
public objObservable: any;
private objObserver: any;
constructor() {
this.objObservable = new Observable((localObserver) => {
this.objObserver = localObserver; // Convert this.objObserver from any to an observer object
this.objObserver.next(this.drugs); // Connect this.drugs to observable object by observer
});
}
getDrugs(): Observable<Drugs> {
return this.objObservable;
}
addDrug(newDrug: Drug) {
this.drugs = [...this.drugs, newDrug];
return this.objObserver.next(this.drugs);
}
removeDrug(neo4jId: string) {
this.drugs = this.drugs.filter((item: Drug) => item.neo4jId !== neo4jId);
return this.objObserver.next(this.drugs);
}
I am using this service to add and remove some Drugs into an Observable in my Home component. In the card Component i want to loop in this Observable and give out the values i need to show in the html.
card.component.html
<div>
{{ this.cardService.getDrugs() | async | json }}
</div>
this one is giving me all the output of the observable as json and then i tried to do something like
<div>
{{ (this.cardService.getDrugs() | async | json).type }}
</div>
to only show me the type, but this is not working. How can i loop through all Objects in this observable and output only the types f.e.?
Drug Model:
export interface Drug {
neo4jId?: string;
drugId?: string;
tradingName: string;
type: string;
quantity: number;
expire: Date;
entryDate: Date;
lastModified?: Date;
slot: number;
substances?: Substance[]
};
export interface Drugs extends Array<Drug> { }