简体   繁体   English

Angular 4更深的数据绑定

[英]Angular 4 Deeper Databind

I have a request from database, and the response is an object, what is included much more data The response result is the next: 我有一个来自数据库的请求,响应是一个对象,包含了更多数据。响应结果是下一个:

ID:{} CATEGORY:{} USERNAME:{} etc...

I've got more CATEGORY TYPE ( user, superuser, analyst, etc... ) I would like to bind the category as like a normal bind. 我还有更多的CATEGORY TYPE(用户,超级用户,分析师等),我想像普通绑定一样绑定类别。 So I don't want to write all category type it individually, I want to bind it. 因此,我不想单独编写所有类别类型,而希望将其绑定。

NOW my code looks like this: 现在我的代码如下所示:

<div *ngFor="let data of data.data ">
   <div *ngIf="data.category === 'analyst'">
      <mat-list>
      </mat-list>     
   </div>
</div>

<div *ngFor="let data of data.data ">
    <div *ngIf="data.category === 'user'">
        <mat-list>
        </mat-list>     
    </div>
</div>

I would like to display data for users on the next way. 我想以另一种方式为用户显示数据。

<div *ngFor="let data of data.data ">
   <div *ngIf="data.category === '{{data.category}}'">
      <mat-list>
      </mat-list>     
   </div>
</div>

As far as I understand, you are trying to access the property, 'category' from the object, data , which contains your iterator data for comparison. 据我了解,您正在尝试从对象data访问属性“ category”,该对象包含要比较的迭代器数据

In that case, you should probably use a different variable name for you iteration and it should be just fine; 在这种情况下,您可能应该为迭代使用不同的变量名,这应该很好。 like, 喜欢,

<div *ngFor="let item of data.data ">
    <div *ngIf="item.category === data.category">
        <mat-list>
        </mat-list>     
    </div>
</div>

What you can do is : 您可以做的是:

  1. Iterate your data list 迭代您的数据列表
  2. Push data.catgory in some array data.catgorydata.catgory某些数组中
  3. Read that array in HTML as: 读取HTML中的该数组为:

    <div *ngFor = "let item in newArr"> <mat-list> </mat-list> </div>

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

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