1

I work on angular and I am unable to assign value to the key. My model is like below

export class aa{
    types: bb[];
}

export interface bb{
    name: string;
    age: string;
}

On my ts file, I did:

    newType: bb;

    initValues(){
    this.newType.name = 'Anna'; //error at this line
}

but I am getting an error as cannot get property of undefined.

Where am I going wrong? How can I assign value to a key in angular

1
  • 1
    newType: any; will do! Interfaces are of no use in javascript at the end! They are just for code writing convenience. Commented Jul 31, 2018 at 9:15

1 Answer 1

1

Now you have only declared the type of your newType property. You need also to initialize your property

newType: bb = { name: null, age: null };

Or you can say that the properties are optional and don't explicitly assign them null

export interface bb{
    name?: string;
    age?: string;
}

newType: bb = { };
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.