ngFor可以在网络上使用,但不能在实际的android设备(离子设备)上使用

问题描述 投票:0回答:1

问题:我的ngfor在运行Web时运行完美,但是当ı在真实设备上模拟我的应用程序时,它只是无法工作。我到处看 互联网寻求解决方案,但找不到,就像一个封闭的(那 我怎么想),有人说这是ngZone问题,我没有任何 知道是什么。

我的服务

getMenuObject(): Observable<any> {

    return this.http
      .post("xxxxxxx.xxxxxxxxx.xxxxxxxxx.xxxxxxxxxxx",{ HotelId:25, GroupId:9 });
  }

我的.ts

data: Array<any>=[]

ngOnInit(){
 this.httpService.getMenuObject().toPromise().then(x=>{
      this.data = x;
      console.log(this.data)
      if(x.IsGroup==true){
          this.splashScreen.hide();

       } else{
         this.router.navigate(['folder/Inbox']);
       }
    });
}

我的html:

<ion-card  *ngFor="let otel of data.MobileHotelDefinitions" style="margin: 40px 0;border-radius: 0;">
        <ion-card-header style="padding: 0;">
          <ion-img (click)="goToHotel()" [src]="otel.MobileApplicationDefinition.GroupImageUrl"></ion-img>
          <div class="otelName">
            <div style="flex: 1;">{{otel.Name}}</div>
            <div style="color: goldenrod;">★★★★★</div>
          </div>
        </ion-card-header>
        <ion-card-content>
          Keep close to Nature's heart... and break clear away, once in awhile,
          and climb a mountain or spend a week in the woods. Wash your spirit clean.
        </ion-card-content>
      </ion-card>

我的控制台:enter image description here

我在网络浏览器上的应用(离子服​​务--o):

enter image description here

我在真实android设备(华为android 9)上的应用:enter image description here

android angular ionic-framework ngfor
1个回答
0
投票
  1. 是否有将HTTP可观察的HTTP转换为Promise的特定需求?尝试直接使用可观察对象。
  2. 使用属性之前,请在模板中包含*ngIf检查。
  3. data变量不是数组。其中的MobileHotelDefinitions属性是数组。因此,最好将其类型定义为any而不是Array
  4. 注意模板中使用safe navigation operator ?.。它会在尝试访问父属性之前检查其父属性是否已定义。

控制器

?.

模板

data: any;    // <-- `any` here

ngOnInit() {
  this.httpService.getMenuObject().subscribe(
    response => {
      this.data = response;
      console.log(this.data);
      if (response.IsGroup == true) {
        this.splashScreen.hide();
      } else {
        this.router.navigate(['folder/Inbox']);
      }
    },
    error => {
      // always good practice to handle HTTP errors
    }
  );
}
© www.soinside.com 2019 - 2024. All rights reserved.