Issue
paraBirims: ParaBirimi[];
Get_All(adres: any): Observable < T[] > {
return this.http.get < T[] > (this.base + "/" + adres);
}
getParaBirims(): ParaBirimi[] {
this.genericParaBirim?.Get_All("ParaBirimis/Generic_Method").subscribe({
next: (data) => {
this.paraBirims = data
},
error: (err) => {},
complete: () => {}
});
}
I want it to give me list as return in above code(getParaBirims). How should I write?
this.paraBirims = getParaBirims();
Solution
I would recommend using the environment.ts file to define your URL prefixes. That way you can change them based on what environment you're running the app.
As dopoto said, it is better to simply define the Observable and subscribe to it in the component. We can remove a lot of boilerplate from your code and do the following.
public paraBirims$: Observable<ParaBirimi[]>;
constructor(private http:HttpClient){
this.paraBirims$ = this.http
.get<ParaBirimi[]>(`${environment.url}/ParaBirimis/Generic_Method`);
}
Then in your component's TS file:
this.paraBirims$ = this.service.paraBirims$;
Lastly in your component's HTML template:
<ul>
<li *ngFor="let paraBirim of paraBirims$ | async">
<!-- do whatever with paraBirim -->
</li>
<ul>
The async pipe will automatically handle subscribing and unsubscribing based on your component's lifecycle.
Answered By - Joshua McCarthy
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.