2015-12-24 69 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'];'(我更新代码) –