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可以帮助:)