[英]Angular2 - *ngIf and *ngFor create multiple empty elements
I have two tables in db, and from the apis, i'm creating loops:我在 db 中有两个表,并且从 apis 中,我正在创建循环:
<div *ngFor="let laptop of laptops">
some content
<!-- Producer / Name -->
<div *ngFor="let producer of producers">
<div class="title" *ngIf="producer.id === laptop.producerId>
{{producer.name}} {{laptop.model}}
</div>
</div>
</div>
I have 70 producer names and foreign key from laptops to producer table.我有 70 个生产者姓名和从笔记本电脑到生产者表的外键。 My eg laptop has got producerId = 3 and i want to loop for each producers and display only this which meets the condition.我的例如笔记本电脑有 producerId = 3,我想为每个生产者循环并仅显示符合条件的。 It works but when i see DOM, it scares me.它有效,但是当我看到 DOM 时,它让我害怕。 70 divs! 70 格! I've read this: *ngIf and *ngFor on same element causing error我读过这个: *ngIf 和 *ngFor 在同一个元素上导致错误
and after this:在此之后:
<ng-container *ngFor="let producer of producers>
<div class="title" *ngIf="producer.id === laptop.producerId>
{{producer.name}} {{laptop.model}}
</div>
</ng-container>
I have in DOM:我在 DOM 中有:
Is that correct result?这是正确的结果吗? Or is there any way to create only that div which meets the condition?或者有没有办法只创建满足条件的div?
As @Gunter already mentioned, this are the placeholder created for binding.正如@Gunter 已经提到的,这是为绑定创建的占位符。 This is normal.这个是正常的。
But I'd love to restructured my data before dumping the HTML, so in terms of performance and readability it will look nice.但我很想在转储 HTML 之前重组我的数据,因此在性能和可读性方面它看起来不错。
//once laptop and producers are retrieved from the server call below method
formatLaptopData(producers) {
this.laptops = this.laptops.map((laptop: any)=> {
laptop.producers =
producers.filter((producer: any) => laptop.producerId == producer.id);
return laptop;
});
}
Html html
<div *ngFor="let laptop of laptops">
some content
<div *ngFor="let producer of laptop.producers">
<div class="title">
{{producer.name}} {{laptop.model}}
</div>
</div>
</div>
We can achieve it by creating a filter.我们可以通过创建一个过滤器来实现它。 Create a file FilterPipe.ts with below codes.使用以下代码创建文件 FilterPipe.ts。
import {Pipe, PipeTransform} from '@angular/core';
@Pipe({
name: 'filter'
})
export class FilterPipe implements PipeTransform {
public transform(value, keys: string, term: any) {
if (!term) return value;
return (value || []).filter(item => keys.split(',').some(key =>
item.hasOwnProperty(key) && new RegExp(term, 'gi').test(item[key])));
}
}
Now the code will be like this现在代码将是这样的
<div *ngFor="let laptop of laptops">
some content
<!-- Producer / Name -->
<div class="title" *ngFor="let producer of producers | filter : 'id' : laptop.producerId">
{{producer.name}} {{laptop.model}}
</div>
</div>
*Don't forget to import filter into the module. *不要忘记将过滤器导入模块。
Rewriting the whole answer, I actually found out the solution for it eventually.重写整个答案,我实际上最终找到了解决方案。 you just need to add ;else null
to the ngIf*
你只需要添加;else null
到ngIf*
<div *ngFor="let producer of producers">
<div class="title" *ngIf="producer.id === 'laptop.producerId'; else null">
{{producer.name}} {{laptop.model}}
</div>
</div>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.