简体   繁体   English

角度* ng用于数据匹配

[英]Angular *ngFor Data Matching

I need help to find the correct way to display my JSON data in Angular 7 template. 我需要帮助以找到在Angular 7模板中显示JSON数据的正确方法。 As you can see in my JSON sample below, I have a person object with multiple identification numbers, I'm using *ngFor to display them in the template, and uses *ngIf to match the type and display the number. 如您在下面的JSON示例中所看到的,我有一个带有多个标识号的person对象,我正在使用* ngFor在模板中显示它们,并使用* ngIf来匹配类型并显示数字。 However, this method will only show fields where data is available, if the student ID is not returned from the JSON object, the whole field including the label will not be displayed. 但是,此方法只会显示有数据可用的字段,如果未从JSON对象返回学生ID,则不会显示包括标签的整个字段。

My first is, am I doing the right thing by using *ngIf to match the id type inside the ngFor? 我的第一件事是,我是否通过使用* ngIf匹配ngFor中的id类型来做正确的事情?

Secondly, is there another way to represent the identification data where if id type is not available, for example the studentID type is not returned , I can use the template to display N/A? 其次,还有另一种方法来表示标识数据,如果id类型不可用,例如未返回studentID类型,则可以使用模板显示N / A?

*UPDATED the duplicated passport mistake *更新重复护照错误

JSON Data Sample JSON数据样本

person = {
  name: { firstname: "John ", lastname: "Smith" },
  identification: [
  { type: "ID", number: "CSG112345", },
  { type: "PASSPORT", number: "AB4455566" }   
  ]}

Template 模板

<ng-container *ngFor="let id of person.identification">
  <p *ngIf="id.type==='ID'">
    <label>ID:</label>
    <b>{{id.number}}</b>
  </p>
  <p *ngIf="id.type==='PASSPORT'"  >
    <label>Passport Number :</label>
    <b>{{id.number}}</b>
  </p>
  <p *ngIf="id.type==='STUDENTID'">
    <label>Student Number :</label>
    <b>{{id.number}}</b>
  </p>
</ng-container>

The *ngIfs arent wrong. * ngIfs不正确。 Just the ngSwitch is much cleaner. 只是ngSwitch更干净。 You can use ternary operator in template to set a default value. 您可以在模板中使用三元运算符来设置默认值。

   <ng-container *ngFor="let id of person.identification">
    <ng-container [ngSwitch]="id.type">
      <p *ngSwitchCase="'PASSPORT'">
        <label>Passport :</label>
        <b>{{id.number ? id.number : 'N/A'}}</b>
      </p>
      <p *ngSwitchCase="'PASSPORT'">
        <label>Passport Number :</label>
        <b>{{id.number ? id.number : 'N/A'}}</b>
      </p>
      <p *ngSwitchCase="'STUDENTID'">
        <label>Student Number :</label>
        <b>{{id.number ? id.number : 'N/A'}}</b>
      </p>
    </ng-container>
   </ng-container>

Also you have duplication of PASSPORT. 另外,您有PASSPORT的副本。 Don't know if that is intended. 不知道这是否是故意的。 Since we are iterating an array to the template, you should provide a default value for none existing members before you display it. 由于我们正在迭代数组到模板,因此在显示模板之前,应为不存在的成员提供默认值。 That would be my aproach. 那是我的方法。 Otherwise you would need to make a fn like 否则,您需要像

checkForType(type:string):boolean{
 return person.identification.map(e => e.type).includes(type)
};

And check for it in the template. 并在模板中检查它。 Something like 就像是

<ng-container *ngIf="checkForType(id.type)">
.....
</ng-container>
<ng-container *ngIf="!checkForType(id.type)">
.....
</ng-container>

But this will create a lot of bloating of the html file. 但这会导致html文件膨胀。 I would suggest you give it a default value before rendering it to the html. 我建议您在将其呈现到html之前为其提供默认值。

<b>{{id.number ? id.number : 'N/A'}}</b>

只需使用此行代替<b>{{id.number}}</b>您将获得预期的输出。

If you want to show each template element, just add condition in <b> itself. 如果要显示每个模板元素,只需在<b>本身中添加条件。 see updated stackblitz. 请参阅更新的stackblitz。 https://stackblitz.com/edit/angular-tqixcb https://stackblitz.com/edit/angular-tqixcb

<p>
   <label>Name :</label>
   <b>{{person.name.firstname}}</b>
</p>
<p>
   <label>ID :</label>
   <b>{{getNumber("ID")}}</b>
</p>
<p>
   <label>Passport Number :</label>
   <b>{{getNumber("PASSPORT")}}</b>
</p>

ts method: ts方法:

  getNumber(type): String {
    const numberValue = this.person.identification.find(x => x.type === type);
    if(!numberValue) {
      return "N/A"
    }
    return numberValue.number;
  }

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

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