带有 *ngIf 内部的模板,更改模型后不起作用 [英] Template with *ngIf inner, not work after change model
问题描述
问题
Angular 2.2 版本之后.*,我注意到我的应用程序的某些组件存在问题,更新数据后,视图中的数据是错误的(它以正确的大小显示了列表,但仅显示了第一项的数据).>
演示
这种类型的使用会导致问题:
<模板pTemplate let-item><b *ngIf="item % 2 == 0">{{item}}</b><del *ngIf="item % 2 != 0">{{item}}</del>模板></list-component>
出现问题的说明:
- 在Plunker中打开示例;
- 观察第二个块(带有
*ngIf
的模板:) - 点击刷新"按钮;
- 再次查看第二个块(带有
*ngIf
: 的模板);
问题
导致此问题的原因是什么以及如何解决?
Inside template with *ng 如果你有多个 TemplateRef
.当数据改变时,它是混合的.
1) 因此,您必须指定 ng-content 中的哪个模板完全用于您的目的,例如:
<模板#tmpl let-item><== 注意#tmpl<b *ngIf="item % 2 == 0">{{item}}</b><del *ngIf="item % 2 != 0">{{item}}</del>模板></list-component>
然后在您的 ListComponent 中:
@ContentChild('tmpl') 模板:TemplateRef;
附言但是你为什么不使用 ngTemplateOutlet
或 ngForTemplate
解决方案?
例如:
2) 而且我在您的示例中注意到 PrimeTemplate
指令.所以这是第二个选择:
@ContentChild(PrimeTemplate) primeTmpl: PrimeTemplate;
和 html:
<template-loader [data]="item" [template]="primeTmpl.template"></template-loader>
Problem
After the version of Angular 2.2. *, I noticed a problem in some components of my application, that after updating the data, the data was wrong in the view (it presented the list with the right size, but only with the data only of the first item) .
Demonstration
I created this Plunker with a simple example of the problem.
This type of use causes the problem:
<list-component [data]="list">
<template pTemplate let-item>
<b *ngIf="item % 2 == 0">{{item}}</b>
<del *ngIf="item % 2 != 0">{{item}}</del>
</template>
</list-component>
Instructions for the problem to occur:
- Open the example in Plunker;
- Obverse the second block (Template with
*ngIf
:) - Click the "Refresh" button;
- And look at the second block (Template with
*ngIf
:) again;
Question
What can be causing this problem and how to solve it?
Inside template with *ngIf you have several TemplateRef
. When data is changed it is mixed.
1) So you have to specify which template within ng-content you are exactly using for your purpose, for example:
<list-component [data]="list">
<template #tmpl let-item> <== notice #tmpl
<b *ngIf="item % 2 == 0">{{item}}</b>
<del *ngIf="item % 2 != 0">{{item}}</del>
</template>
</list-component>
and then in your ListComponent:
@ContentChild('tmpl') template: TemplateRef<any>;
P.S. but why aren't you using ngTemplateOutlet
or ngForTemplate
solutions?
For Example:
2) And i noticed in your example PrimeTemplate
directive. So here is second option:
@ContentChild(PrimeTemplate) primeTmpl: PrimeTemplate;
and html:
<template-loader [data]="item" [template]="primeTmpl.template"></template-loader>
这篇关于带有 *ngIf 内部的模板,更改模型后不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!