Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Getter and Setter of Model object in Angular 4

How can I make getter and setter work in my model class?

My goal is to calculate integer value of the selected day when input, containing the date, updated. I was going to do it in setter, but Angular 4 ignores setter and getter of my model.

My model class:

export class MyModel {     @Input('date')     get date(): String {         console.log('Getting date');         ...     }      set date(val) {         console.log('Setting date: ' + val);         ...     } } 

My template:

... <input class="form-control" name="dp" [(ngModel)]="model.date"> ... 

But getter and setter don't work. What am I missing?

like image 494
AVokin Avatar asked Jun 12 '17 21:06

AVokin


People also ask

What is getter and setter in angular?

A getter method returns the value of the property's value. A getter is also called an accessor. A setter method updates the property's value. A setter is also known as a mutator.

What is getter and setter in typescript?

getter: This method comes when you want to access any property of an object. A getter is also called an accessor. setter: This method comes when you want to change any property of an object. A setter is also known as a mutator.

What is the difference between setter and getter?

Getters and setters are used to protect your data, particularly when creating classes. For each instance variable, a getter method returns its value while a setter method sets or updates its value. Given this, getters and setters are also known as accessors and mutators, respectively.

What is getter () used for?

Getter returns the value (accessors), it returns the value of data type int, String, double, float, etc. For the program's convenience, getter starts with the word “get” followed by the variable name. While Setter sets or updates the value (mutators). It sets the value for any variable used in a class's programs.


1 Answers

The way you declare the date property as an input looks incorrect but its hard to say if it's the only problem without seeing all your code. Rather than using @Input('date') declare the date property like so: private _date: string;. Also, make sure you are instantiating the model with the new keyword. Lastly, access the property using regular dot notation.

Check your work against this example from https://www.typescriptlang.org/docs/handbook/classes.html :

let passcode = "secret passcode";  class Employee {     private _fullName: string;      get fullName(): string {         return this._fullName;     }      set fullName(newName: string) {         if (passcode && passcode == "secret passcode") {             this._fullName = newName;         }         else {             console.log("Error: Unauthorized update of employee!");         }     } }  let employee = new Employee(); employee.fullName = "Bob Smith"; if (employee.fullName) {     console.log(employee.fullName); } 

And here is a plunker demonstrating what it sounds like you're trying to do: https://plnkr.co/edit/OUoD5J1lfO6bIeME9N0F?p=preview

like image 114
SpaceFozzy Avatar answered Oct 04 '22 10:10

SpaceFozzy