DEV Community

Cover image for Angular Getter and Setters Properties in 2020
John Peters
John Peters

Posted on • Updated on

Angular Getter and Setters Properties in 2020

The Typescript Equivalent...

//assume this is our MessageComponent
 _message;
 get message() { return this._message };
 set message(value: string) {
   debugger;
   this._message = value;
 }

//how to use from another component
let mc = new MessageComponent();
mc.message = "You were always on my mind";
let message = mc.message;


Enter fullscreen mode Exit fullscreen mode

Occasionally, we need to see when a value is being set; especially when the property is an @Input() variable. @Input properties accept input from other components.

Good Use Cases

  • We want to put in a debugger statement to find when and which program is making the change.
  • We want to do additional validation than just the type. As for validation don't forget Angular's FormGroup and FormControl validation system. And the upcoming "Decorator methodology".

JWP 2020

Latest comments (0)