admin管理员组文章数量:1318017
I'm trying to figure out how to properly return a list of custom objects and print its contents using an HTML file.
Here's what the HTTP GET method looks like (for simplicity's sake I've stripped some things down to only illustrate the problem) :
[HttpGet("/atr/testing")]
public List<CustomObject> GetCustomObjects()
{
//MasterObject.CustomObjects returns a List of CustomObjects
return MasterObject.CustomObjects();
}
My TypeScript file (called home.ts) subscribes and calls the HttpGet method - here's what the Home class looks like (I've excluded all the imports and @Component section):
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();
});
}
}
As you can see, the constructor calls 'getConfigurations' which calls the HttpGet method. The part that confuses me is how do I go about using the returned response properly? Form my understanding responses are returned as JSON strings (which is why my selectedConfiguration variable in the Home class is a string value instead of a List). When I try to print out the 'selectedConfiguration' string with {{selectedConfiguration}} in my HTML file I get this:
[object Object],[object Object],[object Object],[object Object]
If I use {{selectedConfiguration[0]}} in my HTML I only get one: [object Object]
The List returned by the GET method is supposed to have 4 CustomObject's in it and it seems like the JSON response has that at least. Each CustomObject has variables like 'Name', 'Date', 'Time', etc. I've tried printing data in my HTML using {{selectedConfiguration[0].Name}} and it prints out nothing. I'm not really sure what do to from here (or if I'm doing something wrong) in order to display each object's info.
I'm trying to figure out how to properly return a list of custom objects and print its contents using an HTML file.
Here's what the HTTP GET method looks like (for simplicity's sake I've stripped some things down to only illustrate the problem) :
[HttpGet("/atr/testing")]
public List<CustomObject> GetCustomObjects()
{
//MasterObject.CustomObjects returns a List of CustomObjects
return MasterObject.CustomObjects();
}
My TypeScript file (called home.ts) subscribes and calls the HttpGet method - here's what the Home class looks like (I've excluded all the imports and @Component section):
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();
});
}
}
As you can see, the constructor calls 'getConfigurations' which calls the HttpGet method. The part that confuses me is how do I go about using the returned response properly? Form my understanding responses are returned as JSON strings (which is why my selectedConfiguration variable in the Home class is a string value instead of a List). When I try to print out the 'selectedConfiguration' string with {{selectedConfiguration}} in my HTML file I get this:
[object Object],[object Object],[object Object],[object Object]
If I use {{selectedConfiguration[0]}} in my HTML I only get one: [object Object]
The List returned by the GET method is supposed to have 4 CustomObject's in it and it seems like the JSON response has that at least. Each CustomObject has variables like 'Name', 'Date', 'Time', etc. I've tried printing data in my HTML using {{selectedConfiguration[0].Name}} and it prints out nothing. I'm not really sure what do to from here (or if I'm doing something wrong) in order to display each object's info.
Share Improve this question asked May 17, 2016 at 22:54 Roka545Roka545 3,63623 gold badges70 silver badges111 bronze badges2 Answers
Reset to default 3The short answer
In your example, you're trying to convert a JSON object to a string. To get it to work, you will probably have to do:
this.selectedConfiguration = JSON.stringify(res);
The long answer
Let's say your CustomObject class looks something like:
public class CustomObject
{
public string Name { get; set; }
public DateTime Date { get; set; }
}
In your Angular code, create an interface that mimics it. Verify in developer tools that the case matches ("Name" and not "name"):
export interface ICustomObject {
Name: string;
Date: Date;
}
Leverage the type checking of TypeScript and actually read your returned list as an array of your 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();
});
}
Return it as a json result:
[HttpGet("/atr/testing")]
public JsonResult GetCustomObjects()
{
//MasterObject.CustomObjects returns a List of CustomObjects
return Json(MasterObject.CustomObjects());
}
本文标签: javascriptReturn a List of custom objects from an HTTP GET for use in an Angular 2 appStack Overflow
版权声明:本文标题:javascript - Return a List of custom objects from an HTTP GET for use in an Angular 2 app - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742029702a2416211.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论