Angular 2+ - Set ngModel to null when ngIf causes hide

后端 未结 2 1586
难免孤独
难免孤独 2021-01-26 05:50

I have an issue similar to Reset ngModel values on ngIf in angular2

I would like to set any ngModel values to null whenever a parent *ngIf causes that element to be hidd

2条回答
  •  情深已故
    2021-01-26 06:22

    you can leverage the DoCheck Lifecycle hook to set the values.

    ngDoCheck()
      {
        if(!this.outerBoxVisible)
        {
          this.outerTextValue=null;
          console.log('outertextvalue='+this.outerTextValue);
        }
         if(!this.innerBoxVisible)
        {
          this.outerTextValue=null;
          console.log('innertextvalue='+this.outerTextValue);
        }
      }
    

    Forked Stackblitz

提交回复
热议问题