[英]How to show JSON data in angular version 2.4.10
I am trying to show json data by Angular. 我试图通过Angular显示json数据。 But it is showing entire json data instead particular one.
但是它显示的是整个json数据,而不是特定的数据。
Case 1 : 情况1 :
import { Component, OnInit } from '@angular/core';
import { HttpService } from 'app/http.service';
import { Response } from '@angular/http/src/static_response';
@Component({
selector: 'app-httpapp',
template: `
<p>
httpapp Works!
<br><hr>
{{t_obj | json}}
</p>
<div *ngFor="let t of t_obj">
{{t.tname}}//not working
{{t}}
</div>
`,
})
export class HttpappComponent implements OnInit {
t_obj : T ;
constructor(private httpService : HttpService) { }
ngOnInit()
{
this.httpService.getData()
.subscribe(
(data : Response) => {
this.setData(data)
}
);
}
setData(response : Response)
{
this.t_obj = response.json();
console.log(response.json());
}
}//class
class T{
tname : string;
}
output: 输出:
[ { "t": "A" }, { "t": "B" }, { "t": "C" } ]
[object Object]
[object Object]
[object Object]
{{t.tname}}, this fragment of code showing nothing. {{t.tname}},此代码片段未显示任何内容。
Below is my server side code : 下面是我的服务器端代码:
@RestController
public class NewClass
{
@RequestMapping("/greeting")
public List greeting()
{
List l = new ArrayList();
l.add(new T("A"));
l.add(new T("B"));
l.add(new T("C"));
return l;
}
}//public class NewClass
class T
{
public String t;
public T(String t)
{
this.t = t;
}
}
Now i want to know that how can i show only data instead of the full json object. 现在我想知道如何显示仅数据而不是完整的json对象。
Case 2 : 情况2:
Below is my REST controller : 以下是我的REST控制器:
@RequestMapping("/greeting")
public T greeting()
{
return new T("Done..!");
}
Angular code : 角度代码:
@Component({
selector: 'app-httpapp',
template: `
<p>
httpapp Works!
<br><hr>
{{t_obj.t}}
</p>
`,
})
export class HttpappComponent implements OnInit {
t_obj : any ;
constructor(private httpService : HttpService) { }
ngOnInit()
{
this.httpService.getData()
.subscribe(
(data : Response) => {
this.setData(data)
}
);
}
setData(response : Response)
{
this.t_obj = response.json();
console.log(response.json());
console.log(this.t_obj.t);
}
}//class
For case two i am getting the below error message : 对于第二种情况,我收到以下错误消息:
EXCEPTION: Uncaught (in promise): Error: Error in ./HttpappComponent class HttpappComponent - inline template:3:14 caused by: Cannot read property 't' of undefined
Error: Error in ./HttpappComponent class HttpappComponent - inline template:3:14 caused by: Cannot read property 't' of undefined
After showing this error message it is printing the below lines : 显示此错误消息后,它正在打印以下行:
{t: "Done..!"}
Done..!
So my question is, according to the error message if 't' is undefined then how it is being printed in console log. 所以我的问题是,根据错误消息,如果未定义“ t”,那么它将如何在控制台日志中打印。
Update : I got solution for case 1, i have to use {{tt}} 更新:我有情况1的解决方案,我必须使用{{tt}}
根据您的代码,它应该是{{tt}},而不是{{t.tname}}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.