简体   繁体   中英

Can I define events in Typescript?

Let's say I have a parent Component app-main-console and a nested component app-console . The nested component has a table. It's initial when the main component loads.

I want data to populate the table be fetched if user wants to see it. So there's a input property called fetchTableData .

<app-console [fetchTableData]="fetchTableData">          
</app-console>

It's a boolean.

@Input() fetchTableData: boolean;

Is it possible to define an event so that when the value of fetchTableData becomes true , I can call the backend service?

Thanks for helping

You can use OnChanges from Angular life cycle hook .

ngOnChanges(changes: SimpleChanges) {
  for (let propName in changes) {
    if(propName === 'fetchTableData') {
      let chng = changes[propName];
      let cur  = chng.currentValue;
      let prev = chng.previousValue;

      if(cur && cur === true) {
        // Action you want to perform
      }

    }
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM