Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Angularjs 使用Typescript和Angular获取Json_Angularjs_Json_Typescript - Fatal编程技术网

Angularjs 使用Typescript和Angular获取Json

Angularjs 使用Typescript和Angular获取Json,angularjs,json,typescript,Angularjs,Json,Typescript,我有这个接口和这个类: module AnimalPlanet{ export interface Pet { Type: string; Name: string; Age: string; Color: string; SpecialCare: boolean; AvailableForAdoption: boolean; LDAdoption: boolean;

我有这个接口和这个类:

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方法不起作用。可能是我的类写错了。你能帮我吗?塞纳克斯

的确,有些地方出了问题。您必须将http调用的响应分配给作用域的属性,而不是执行
返回xxx

您的Ctrl键必须类似于:

导出类petsCtrl{
dataFromServer:RootObject;
构造函数(private$http:ng.IHttpService,private$scope:any){
这是.http.get('/app/pets/pets.json')。然后(函数(响应){
console.log(response.data);
这.$scope.pets=response.data;
});
}
}

petsCtrl.$inject=['$http','$scope']的确,有点不对劲。您必须将http调用的响应分配给作用域的属性,而不是执行
返回xxx

您的Ctrl键必须类似于:

导出类petsCtrl{
dataFromServer:RootObject;
构造函数(private$http:ng.IHttpService,private$scope:any){
这是.http.get('/app/pets/pets.json')。然后(函数(响应){
console.log(response.data);
这.$scope.pets=response.data;
});
}
}

petsCtrl.$inject=['$http','$scope']我收到了如下json:[Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object“被所有者抛弃”LDAdoption:true Name:“Catty”SpecialCare:true Type:“cat”featured:true imageUrl:“img/photo_latest14.jpg”最新:true proto:object和一个错误:TypeError:无法设置unfinedsorry的属性“pets”,您必须更改inject属性:
petsCtrl.$inject=['$http','$scope'];
(我更新了代码)好的。现在没有错误,但在我看来什么都没有发生。{{pet.Name}{{pet.Type}{{pet.Name}}你能在http回调中设置一个断点并检查是否有数据吗?也许一点JSFIDLE/plunkr可以帮助:)我收到一个json,如下所示:[Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object,Object]0:Object年龄:““LDAdoption:true Name:“Catty”SpecialCare:true Type:“cat”featured:true imageUrl:“img/photo_latest14.jpg”最新:true proto:object和一个错误:TypeError:无法设置未定义的属性“pets”sorry,您必须更改inject属性:
petsCtrl.$inject=['$http','$scope'];
(我更新了代码)好的。现在没有错误,但在我看来什么都没有发生。{{pet.Name}{{pet.Type}{{pet.Name}}你能在http回调中设置一个断点并检查是否有数据吗?也许一点JSFIDLE/plunkr可以帮助:)