In the example of creating a custom structural directive, why do we need the flag "this.hasView"?

Viewed 23

In the documentation of creating a structural directive. As I understand, the flag hasView is used to ensure the embedded view was not yet inserted into the view container before inserting it.

@Input() set appUnless(condition: boolean) {
  if (!condition && !this.hasView) {
    this.viewContainer.createEmbeddedView(this.templateRef);
    this.hasView = true;
  } else if (condition && this.hasView) {
    this.viewContainer.clear();
    this.hasView = false;
  }
}

But why? It seems unnecessary, because if we remove this flag, I don't see any case the call this.viewContainer.createEmbeddedView(this.templateRef); will be executed twice continuously.

0 Answers
Related