有没有办法在ngIf属性被赋值为true后将其删除?
例如,假设我有一个模板
<div class="container">
<div *ngFor="let el of elements">
<div *ngIf="el.index = index">
...
</div>
</div>
</div>在组件I中有计数器
class Test implements onInit{
index = 0;
ngOnInit(){
setInterval( () => index++ )
}
}这意味着一次只显示一个div,但是我希望之前显示的所有div都保持显示状态,换句话说,一旦ngIf属性被评估为真,就删除它。
这样的事情有可能发生吗?
谢谢
发布于 2016-08-04 18:25:50
这是不可能的。您应该维护组件中的元素列表,以包含*ngFor应该呈现的内容。
发布于 2016-08-04 20:16:53
您可以创建自己的ngIf:
@Directive( { selector : '[myNgIf]' } )
export class myNgIfDirective {
private condition;
constructor ( private templateRef : TemplateRef<any> ,
private viewContainer : ViewContainerRef ) {
}
@Input() set myNgIf ( condition : boolean ) {
if ( this.condition !== true ) {
// if this condition is once set to true , don't bother doing anything anymore
this.condition = condition;
if ( condition ) {
this.viewContainer.createEmbeddedView( this.templateRef );
} else {
this.viewContainer.clear();
}
}
}
}您可以使用它:
<div *myNgIf ="isTrue">stuff</div>https://stackoverflow.com/questions/38764750
复制相似问题