Joshua
Joshua

Reputation: 21

angular 2 how to add a model within model

I am getting a response json from API like below.

student: { 
           name : abc, 
           address : { 
                      city : ca, 
                      state:abc 
                      }, 
           age : 10 
         }

In order to bind this to a model, i need a model similar to this

class student {
      name:string;
      age:number;
      address:{ 
               city:string; 
               state:string
              }
       }

But when I bind the data to the above model. Address data is not binding to model. Please suggest the right way to write a model to bind the above data.

Upvotes: 1

Views: 2019

Answers (2)

AngularChef
AngularChef

Reputation: 14087

You need to instantiate an instance of the Student class, i.e.

class Student {
  // Define Student properties here
  name:string;
  age:number;
  // ...

  constructor(options) {
    // Set Student properties here
    this.name = options.name;
    this.age = options.age;
    this.address = options.address;
    // ...
  }
}

// Then:
const student = new Student(jsonData);

Upvotes: 0

anshuVersatile
anshuVersatile

Reputation: 2068

export class A {
  name:string;
  age:number;
  address:Adress;
   }


export class Adress  
           city:string; 
           state:string

   }

Upvotes: 1

Related Questions