简体   繁体   English

离子:ngFor 中的 ngIf 语句不起作用

[英]Ionic : ngIf statement inside a ngFor not working

I want to display data from database with the help of ngfor.我想在 ngfor 的帮助下显示数据库中的数据。 I want to use ngif for implement some display condition .我想使用 ngif 来实现一些显示条件。

my code :我的代码:

<ion-grid  *ngFor="let chat of ChatListArray">
  <ion-row *ngIf="chat.status == 'Incoming'">
    <ion-col >
      <div>
       {{chat.msg}}
      </div>
    </ion-col>
  </ion-row>
  <br>
  <ion-row *ngIf="chat.status == 'Outgoing'"> 
    <ion-col >
      <div>
       {{chat.msg }}
      </div>
    </ion-col>
  </ion-row>
</ion-grid>

But its not working .但它不工作。 how to use if statement properly please help.如何正确使用if语句请帮忙。

{{chat.msg }} does not display any data. {{chat.msg }} 不显示任何数据。 but outside the ngif its works.但在 ngif 之外它的作品。

My json array :我的 json 数组:

在此处输入图片说明

Your code looks ok and it should work.你的代码看起来没问题,它应该可以工作。 It looks like you have typo into ChatListArray or Incomming .它看起来像你有错字到ChatListArrayIncomming Maybe Incoming ?也许Incoming

What you can do is to use json pipe to see values of chat.status to understand what values you are iterating:您可以做的是使用json管道查看chat.status值以了解您正在迭代的值:

<ion-grid  *ngFor="let chat of ChatListArray">
    <div>
       {{chat | json }}
    </div>
</ion-grid>

Your array contains just one object messages, so this is a reason why you cannot see any messages.您的数组仅包含一个对象消息,因此这就是您看不到任何消息的原因。 Eg ngIf=chat.status == 'Incoming' , it works and it will get this object {"status":"Outgoing"} , but this object does not have any msg object, because this object msg is the next in an iteration:例如ngIf=chat.status == 'Incoming' ,它可以工作并且它会得到这个对象{"status":"Outgoing"} ,但是这个对象没有任何msg对象,因为这个对象msg是迭代中的下一个:

[
{"id":1},
{"number":"+..."},
{"status":"Outgoing"},
{"msg": "hello"},

{"id":2},
{"number":"+..."},
{"status":"Incoming"},
{"msg": "hello"},

{"id":3},
{"number":"+..."},
{"status":"Outgoing"},
{"msg": "hello"},
]

It will work, if you have such objects in your array:如果您的数组中有这样的对象,它将起作用:

[
{"id":1, "number":"+...", "status":"Outgoing", "msg": "hello"},

{"id":2, "number":"+...", "status":"Incoming", "msg": "hello"},

{"id":3, "number":"+...", "status":"Outgoing", "msg": "hello"}
]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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