What is the Angular equivalent to an AngularJS “$watch”?

Technology CommunityCategory: AngularWhat is the Angular equivalent to an AngularJS “$watch”?
VietMX Staff asked 3 years ago

The solution is the set syntax from ES6. The set syntax binds an object property to a function to be called when there is an attempt to set that property.

import { Component, Input } from '@angular/core';
@Component({
  selector: 'example-component',
})
export class ExampleComponent {
  public internalVal = null;
  constructor() {}
  
  @Input('externalVal')
  set updateInternalVal(externalVal) {
    this.internalVal = externalVal;
  }
}