Oudstand Oudstand - 1 year ago 68
TypeScript Question

Additional information stored in object

In my Angular 2 application I've got a class Guide :

export class Guide{
id: number;
name: string;

When I store data in an object of this class, Angular creates more variables, depending of the response.
If I get a response like:

"id": 1,
"name": "Guide",
"created_at": "2016-11-04T08:50:39+0000"

then my object of type
has also the variable "created_at" with the value of "created_at".

How can I just store the two variables
in my object?

Answer Source
this.guide = guide;

Is overwriting your object with a new object guide it is not casting it that is why you are getting additional values. Objects work by reference and you are overwriting the reference.

If you are using typescript try something like:

this.guide = <Guide>guide;

Although i am not sure this will eliminate the other values.

The other alternative is that you need to build a constructor onto Guide object

export class Guide{
    this.id = object.id;
    this.number = object.number;

   id: number;
   name: string;

then you call it like this

this.guide = new Guide(guide);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download