I am looking at the source code *ngIf:
@Input()
set ngIf(condition: any) {
if (condition && !this._hasView) {
this._hasView = true;
this._viewContainer.createEmbeddedView(this._template);
} else if (!condition && this._hasView) {
this._hasView = false;
this._viewContainer.clear();
}
}
Can I have a component that I like as below?
@Component({})
class MyComponent{
constructor (
public _template : TemplateRef,
public _viewContainer : ViewContainerRef) {
}
onSomeButtonClick(condition){
if(condition){
removeMyView();
}else{
putTheViewBackIfItsRemoved();
}
}
}
Trying to use logic ngIfinside a component does not work, which I think because viewContainerRef for the component is empty
EDIT:
Just to mention that I don't want to hide the view, I just want to remove it from the DOM.
In other words, can we be something like ngIfhost elements? I know that you cannot place the directive on host, so I thought that perhaps with ViewContainerand TemplateRefyou can achieve the same.
, , Angular , ViewContainerRef DOM; , Angular ?
, - , ?
, Angular ( ), , , ngIf :
, ngIf :
:
<div *ngIf="condition"></div>
, , , , , div, , , .
ngIf.
:
:
, ngIf :
@Component({
host:{
'*ngIf':'shouldBeRemoved'
}
})
class MyComponent{
, ngIf , , , viewContainerRef - .
, , ngIf ngIf , , .
.