user2599052
user2599052

Reputation: 1136

behaviour of ngIf and property binding in angular

      <myview [myVariable]=myVariable  *ngIf="myVariable">
      </myview>

I have the above component template defined which binds myVariable property to myVariable prop of myview component. If there is a change in myVariable, is the myview component supposed to reload(constructor or ngOnInit). Its not in my environment and thus confused if this is the expected behaviour. The component is getting instantiated just once and I would like it to refresh its values after any change to myVariable in the parent component.

Upvotes: 1

Views: 566

Answers (1)

Yash Rami
Yash Rami

Reputation: 2327

you can get that event in ngOnChanges here is the example.

you need to write ngOnChanges in your myview component

import { Component, OnInit, Input, AfterViewInit, OnChanges, SimpleChanges, ViewChild, Output, EventEmitter } from '@angular/core';
@Input() myVariable: any; 

class myViewComponent implements OnChanges, OnInit {
  ngOnChanges(changes: SimpleChanges): void { 
   console.log(changes.myVariable); // here you will get the updated value of myVariable 
  }
}

you parent component html

 <myview [myVariable]="myVariable"  *ngIf="myVariable">
 </myview>

Upvotes: 1

Related Questions