I have the following function to return data from an Angular 9 service:
@Injectable({
providedIn: 'root'
})
export class SecurityService {
constructor(private http: HttpClient) { }
public getFunds(): Observable<any> {
// could do a loader here, or in an interceptor - same for the post, put and delete
let data = this.http.get<any>("https://localhost:44337/security/").pipe(
map(response => response)
);
return data;
} }
The service returns data as expected. I call it like this:
export class AppComponent {
funds$!: Observable<any>; // "!" turns off check to make sure vars are initialized, not good tho
constructor(private securityService: SecurityService) {}
ngOnInit() {
this.funds$ = this.securityService.getFunds();
this.securityService.getFunds().subscribe(f => (this.funds$ = f));
}
}
In my html, I try to use the data like this:
<tr *ngFor="let f of funds$">
<td>{{ f.id }}</td>
<td>{{ f.name }}</td>
<td>{{ f.assets }}</td>
</tr>
I get this error: TS2322: Type 'Observable' is not assignable to type 'any[] | Iterable | (Iterable & any[]) | (any[] & Iterable) | null | undefined'. Type 'Observable' is not assignable to type 'any[] & Iterable'. Type 'Observable' is not assignable to type 'any[]'.
This used to work a few years ago. Evidently Angular 9 changed something...