loading...
Cover image for Angular Getter and Setters Properties in 2020

Angular Getter and Setters Properties in 2020

jwp profile image John Peters Updated on ・1 min read

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;


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

Discussion

pic
Editor guide