2015-12-24 71 views
1

我有這個接口和這個類:與打字稿得到JSON和角

module AnimalPlanet{ 
    export interface Pet { 
     Type: string; 
     Name: string; 
     Age: string; 
     Color: string; 
     SpecialCare: boolean; 
     AvailableForAdoption: boolean; 
     LDAdoption: boolean; 
     History?: string; 
     featured: boolean; 
     newest: boolean; 
     imageUrl: string; 
     type: string; 
     name: string; 
    } 

    export interface RootObject { 
     pets: Pet[]; 
    } 

    export class petsCtrl { 
     dataFromServer : RootObject; 
     constructor(private $http: ng.IHttpService) { 
      this.$http.get('/app/pets/pets.json').then(function(response){ 
       console.log(response.data); 
        return response.data; 

       }); 
     } 
    } 

    petsCtrl.$inject = ['$http']; 
    app.controller("petsCtrl", petsCtrl);   
} 

我嘗試在視圖來顯示一些數據,但一些沒有按牛逼的工作與我的GET method.Maybe我的課是錯寫。你能幫我嗎? Thnaks!

回答

0

的確,有什麼不對。您必須將您的http呼叫的回覆分配給您的示波器屬性,而不是執行return xxx

您按Ctrl必須是這樣的:

export class petsCtrl { 
 
     dataFromServer : RootObject; 
 
     constructor(private $http: ng.IHttpService, private $scope: any) { 
 
      this.$http.get('/app/pets/pets.json').then(function(response){ 
 
       console.log(response.data); 
 
        this.$scope.pets = response.data; 
 

 
       }); 
 
     } 
 
    } 
 

 
petsCtrl.$inject = ['$http', '$scope'];

然後在您的視圖中,可以顯示屬性的寵物:

<div data-ng-bind="pets | json"></div>

+0

我收到一個json是這樣的:[對象,對象,對象,對象,對象,對象,對象,對象,對象,對象,對象,對象,對象,對象,對象,目標,對象] 0:對象 年齡: 「5-6歲」 AvailableForAdoption:真 顏色: 「棕色」 歷史: 「遺棄所有者」 LDAdoption:真 名稱: 「凱蒂」 SpecialCare:真 類型: 「貓」 特色:真 IMAGEURL: 「IMG/photo_latest14.jpg」 最新:真 __proto__:對象 –

+0

和一個錯誤:類型錯誤:無法設置屬性「寵物'未定義 –

+0

對不起,您必須更改注入屬性:'petsCtrl。$ inject = ['$ http','$ scope'];'(我更新代碼) –