0

Ts

this.myservice.online_service('online_service',data).subscribe(res=>{
        this.result = res;

        this.bus_data=this.result.searchResult;
    })

Html

<ion-card  *ngFor= "let bus of bus_data ; let i = index" [attr.data-target]="'#viewseat' + i" (click)="get_seats(i)">
        <ion-card-header>
            <ion-row>
                <ion-col col-6 class="p0 bus_name">{{bus.operatorName}}</ion-col>
 </ion-row>
</ion-card-header>
 </ion-card>

service.ts

online_service(url,data = null )
{

this.show_loader();
return this.http.post(this.webservice_url+ url,  JSON.stringify(data)).map(res=>res.json())
}

The code works well if server returns multiple data .But getting this error in case server return single data

3 Answers 3

1

Which means you need to always use array to bind with ngFor. you could create a local variable and declare it as an array and then push the result.

myResult : any = [];

and then,

this.myservice.online_service('online_service',data).subscribe(res=>{
        this.result = res;
        myResult.push(this.result.searchResult);
})
Sign up to request clarification or add additional context in comments.

1 Comment

Also use myResult with ngFor
1

You need to push them to the array as follows.

In your component.ts define the array like this.

bus_data: any[] = [];

Now, inside your data loading function,

this.myservice.online_service('online_service',data).subscribe(res=>{
   this.result = res;
   this.bus_data=[...this.result.searchResult];
})

1 Comment

Now it displays in case of single data , and shows empty in case of multiple data. <ion-content class="background" *ngIf="bus_data" [ngClass]="lang=='ar' ? 'ar' : ''"> <ion-card *ngFor= "let bus of bus_data ; let i = index" [attr.data-target]="'#viewseat' + i" (click)="get_seats(i)"> <ion-card-header> <ion-row> <ion-col col-6 class="p0 bus_name">{{bus.operatorName}}</ion-col> . Same bus_data[] declared as array
0

Thank a lot for your answers. Appreciate your efforts. I forget to check array type of data that come from server.Now it works perfectly

Let me post the right answer

this.myservice.online_service('online_service',data).subscribe(res=>{
      this.result = res;

      this.bus_data=this.result;
 if (this.bus_data.code === '1') {
    if (this.bus_data.searchResult instanceof Array !== true) {
       this.bus_data.searchResult = new Array(this.bus_data.searchResult);
   }}
   console.log(this.bus_data);
  })

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.