从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
Return a List of custom objects from an HTTP GET for use in an Angular 2 app
我正试图找出如何正确返回自定义对象列表并使用HTML文件打印其内容。
以下是HTTPGET方法的样子(为了简单起见,我只简化了一些内容来说明问题):
[HttpGet("/atr/testing")]
public List<CustomObject> GetCustomObjects()
{
//MasterObject.CustomObjects returns a List of CustomObjects
return MasterObject.CustomObjects();
}
我的TypeScript文件(称为home.ts)订阅并调用HttpGet方法-home类如下(我已经排除了所有导入和@Component部分):
export class Home {
public selectedConfiguration: string = 'TESTVALUE';
constructor(private http: Http) {
this.getConfigurations()
.subscribe(res => {
this.selectedConfiguration = res;
});
}
getConfigurations() {
console.log('Home:getConfigurations entered...');
return this.http.get('atr/testing')
.map((response) => {
console.log(response);
return response.json();
});
}
}
正如您所看到的,构造函数调用"getConfigurations",它调用HttpGet方法。让我困惑的是,我该如何正确使用返回的响应?根据我的理解,响应以JSON字符串的形式返回(这就是为什么Home类中的selectedConfiguration变量是字符串值而不是List)。当我试图在HTML文件中用{{selectedConfiguration}}打印出"selectedConfiguration"字符串时,我得到的是:
[object Object],[object Object],[object Object],[object Object]
如果我在HTML中使用{{selectedConfiguration[0]}},我只得到一个:〔object object〕
GET方法返回的List中应该有4个CustomObject,而JSON响应似乎至少有这个。每个CustomObject都有"Name"、"Date"、"Time"等变量。我曾尝试使用{{selectedConfiguration[0].Name}}在HTML中打印数据,但它什么也不打印。我真的不确定从这里做什么(或者我是否做错了什么)来显示每个对象的信息。
简短答案
在您的示例中,您试图将JSON对象转换为字符串。为了让它发挥作用,你可能必须做:
this.selectedConfiguration = JSON.stringify(res);
答案很长
假设您的CustomObject类看起来像:
public class CustomObject
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
在Angular代码中,创建一个模仿它的接口。在开发工具中验证大小写是否匹配("Name"而不是"Name"):
export interface ICustomObject {
Name: string;
Date: Date;
}
利用TypeScript的类型检查,将返回的列表实际读取为CustomObjects:的数组
export class Home implements OnInit {
public configurations: ICustomObject[];
public selectedConfiguration: ICustomObject;
constructor(private http: Http) {
// move initialization code to ngOnInit. Don't forget the import and implements
}
ngOnInit() {
this.getConfigurations()
.subscribe((customList: ICustomObject[]) => {
this.configurations = customList;
// todo: determine the selected configuration
this.selectedConfiguration = this.configurations[0];
});
}
getConfigurations(): ICustomObject[] {
return this.http.get('atr/testing')
.map((response: Response) => {
return <ICustomObject[]>response.json();
});
}
将其作为json结果返回:
[HttpGet("/atr/testing")]
public JsonResult GetCustomObjects()
{
//MasterObject.CustomObjects returns a List of CustomObjects
return Json(MasterObject.CustomObjects());
}
- angular 1.5应用程序中的导航栏
- 从HTTPGET返回一个自定义对象列表,以便在Angular 2应用程序中使用
- 没有在Angular应用程序中定义firebase(在firebase迁移之后)
- Grunt构建导致Angular应用程序在dist上崩溃
- 在Angular应用程序中每个帖子投票一次
- 微软边缘浏览器缓存中的RESTful Angular应用程序
- 如何在给定的angular应用程序中获取所有指令名称
- 同一'angular应用程序'在页面上
- RequireJS正在加载angular应用程序,但没有't创建app.controller
- angular应用程序中的浏览器功能检测:使用服务、指令、控制器
- 最基本的 Angular 应用程序无法正常工作
- 如何使用已经存在的 REST API 后端在 Angular 应用程序中实现登录系统
- Angular应用程序不加载初始页面
- 读取Angular应用程序中请求的url
- 如何在滚动时将jQuery fadeIn添加到Angular应用程序中
- IE窗口失去了对任何XMLHttpRequest启动Angular应用程序的关注
- 我只能将TypeScript用于Angular应用程序的部分吗
- IBM Filenet如何与Angular应用程序集成
- 如何在同一页面上的Angular应用程序和非Angular应用软件之间进行通信
- Chrome标签或书签滚动会影响angular应用程序