2017-01-24 35 views
0

我從API獲得如下的響應json。angular 2如何在模型中添加模型

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

爲了結合這種到一個模型,我需要與此類似

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

模型但是,當我將數據綁定到上述模型。地址數據不綁定到模型。 請建議編寫模型的正確方法來綁定上述數據。

+0

你所說的「結合數據模型」是什麼意思?你目前的代碼是「將數據綁定到模型」?你的意思是說你想讓你的數據('學生')成爲班級的一個實例嗎? – AngularChef

+0

高級別綁定代碼 - studentobj = response.json()。響應數據不存儲在地址屬性 – Joshua

+0

啊好的。 「單詞」綁定在角度背景下具有誤導性。這不是Angular中的綁定。 – AngularChef

回答

1
​​
+0

地址類拼寫錯誤否則好 –

+0

哈哈哈編碼器總是錯過Spelings :) – anshuVersatile

+0

我試過這種方式,但不工作... – Joshua

0

您需要實例化Student類的實例,即

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);