[英]How do i pass data received from angular to html front-end?
this is my angular service: 这是我的角度服务:
@Injectable()
export class ApiService {
private get_msg_url: string = "http://localhost:3000/getmessages";
constructor(private http:HttpClient) { }
getMessage(): Observable<IPosts> {
return this.http.post<IPosts>(this.get_msg_url,{"data":"data1"});
}
}
this is my message.component.ts : 这是我的message.component.ts:
export class MessagesComponent implements OnInit {
data = [];
iposts: IPosts[];
constructor(private apiSerivce: ApiService) {
}
getMessage(): void {
this.apiSerivce.getMessage()
.subscribe(data1 => this.data.push(data1));
}
ngOnInit(): void {
this.getMessage();
console.log(this.data);
}
}
this is my posts.ts: 这是我的posts.ts:
export interface IPosts {
timestamp : number;
message_content : string;
message_link :string;
tags : string[] ;
}
this is messsage.component.html: 这是messsage.component.html:
<p>Json data {{data}} </p>
whenever the code is run i can see the required data in console of chrome but there is no data shown on page. 每当运行代码时,我都可以在chrome控制台中看到所需的数据,但是页面上没有显示数据。 the data received on console is of form:
在控制台上收到的数据具有以下形式:
[]
0:
message: Array(3)
0: {timestamp: 1522072833748,
tags: Array(2), _id: "5aacb7cc0281b558debacf26",
message_link:"String"
}}
the problem is, that you try to print an array, instead of the elements. 问题是,您尝试打印一个数组,而不是元素。
<p>Json data {{data}} </p>
change this to something like this: 将此更改为以下内容:
<p *ngFor="let element of data; index as i">
Json data #{{i}}: {{element]]
</p>
As I can see, data is an array. 如我所见,数据是一个数组。 you can display the raw content of an array with the pipe json :
{{data | json}}
您可以使用管道json显示数组的原始内容:
{{data | json}}
{{data | json}}
However I am not sur what you want to do with a json displayed in your html. 但是,我不是想对HTML中显示的json做什么。
The problem is your output data is an array, not an element. 问题是您的输出数据是数组,而不是元素。
Try something like this, 试试这个
<ul *ngFor="let element of data">
<li>{{element}}</li>
</ul>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.