简体   繁体   中英

Convert an array to Observable Object to normal array of object in Angular 4

I found lot of references to convert array of object to observable array of object, but not reverse from observable array of object to normal array of object?

This code return :

myObjects: myObjects[] = this.myService.getMyObjects();

TS2322:Type 'Observable< myObjects[]>' is not assignable to type 'myObjects[]'. Property 'length' is missing in type 'Observable< myObjects[]>'.

How can I convert / cast ?

Thanks

You have to subscribe to it and store the value in a variable.

WARNING

This is an asynchronous call, meaning that your data will be empty until you loaded everything.

myObjects: myObjects[] = [];

ngOnInit(){
    this.myService.getMyObjects().subscribe( objects => {
        this.myObjects = objects;
    });
}

As it seems to be an http call, once the http call has finished, your data will be in the myObjects variable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM