简体   繁体   English

如何在Angular 7的* ngFor中使用* ngIf?

[英]How to use *ngIf in *ngFor in Angular 7?

I have multiple items in my DocumentModel document, which I all want to show except of 'content'. 我的DocumentModel文档中有多个项目,除“内容”外,我都想显示。 I already tried to exclude it by checking the name of the item, but it's showing it anyways: 我已经尝试通过检查商品名称来排除它,但是无论如何它都在显示它:

<p *ngFor="let item of (document | keyvalue)">
    <b *ngIf="item.key != 'content'">{{item.key}}:</b> {{item.value}}
</p>

I also tried using content without '', which is also not working. 我也尝试使用不带''的内容,这也不起作用。

How can I make something like if(!key.equals("content")) as in Java? 如何在Java中制作类似if(!key.equals("content"))

Document Model 文件模型

export class DocumentModel 
{ 
    messageType: string; 
    content: string; 
    failed: boolean; 
    string1: string; 
    string2: string; 
}
You can create pipe to access key

import { PipeTransform, Pipe } from '@angular/core';

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
  transform(value, args:string[]) : any {
    let keys = [];
    for (let key in value) {
      keys.push({key: key, value: value[key]});
    }
    return keys;
  }
}
html:
<span *ngFor="let item of content | keys">           
   <b *ngIf="item.key != 'content'">{{item.key}}:</b> {{item.value}}
</span>

Or 要么

Write filter for content data from list. 为列表中的内容数据编写过滤器。

Here is your initial code: 这是您的初始代码:

<p *ngFor="let item of (document | keyvalue)">
    <b *ngIf="item.key != 'content'">{{item.key}}:</b> {{item.value}}
</p>

But if I look at the *ngFor it seems that item can be a document or a keyvalue . 但是,如果我看*ngFor ,似乎该项目可以是documentkeyvalue So my question is: Are you sure that document and keyvalue has the field key ? 所以我的问题是:你确信documentkeyvalue有场key

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

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