I'm using Angular 7 (and one day I have to upgrade my version). I have a service that have some variables that can change according to some Promise (http GET, PUT, ... response).
I wish to print these variables on a template.
Can I do this:
app.component.html:
<ng-container *ngIf="this.dogService.isWarningProblem">
<ngb-alert [dismissible]="false" type="warning" style="text-align: center">
{{this.dogService.errorMessage}}
</ngb-alert>
</ng-container>
app.service.ts:
export class DraftService {
public errorMessage: string;
public isWarningProblem: boolean;
constructor
(
private generalErrorService: GeneralErrorService,
private http: HttpClient
) {
[...]
}
public launchPingEditReadDraftByActionOfferIdUrl(action: string, offerIdUrl: string): Subscription {
return interval(10).subscribe(
() => {
//Get variables from the server and set them.
},
() => {}
);
}
}
I wish to use service because the algorithm is equal to another components but they cannot see the variables of others components. So, I can not use the subscription with Behavior Subject and Observable:
Are there any better solutions?
No, it is not the
best practiceto render the service result directly into the template. It's better to inject yourserviceas adependency(Dependency Injection) into your component which set somevariableswith service result and render those in your template. So try something like this:app.service.ts
app.component.ts
app.component.html