简体   繁体   English

如何将从angular接收到的数据传递到html前端?

[英]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.

相关问题 how do i pass a datatable from a .net core 5.0 web api to an angular front end as json? - how do i pass a datatable from a .net core 5.0 web api to an angular front end as json? 如何将JSON文件从后端传递到前端 - How to pass a JSON file from the back-end to the front-end 将从spring-boot检索的数据转换为JSON并在有角度的前端中获取 - Convert data retrieved from spring-boot to JSON and fetch it in angular front-end Django-将JSON数据与HTML一起传递到前端不起作用 - Django - passing JSON data to the front-end along with HTML not working 从 Flask 发送 JSON 数据到前端 - Sending JSON data from Flask to Front-end 我应该如何在 javascript 中构造一个数据树,以便它可以被前端解析? - How should I structure a data tree in javascript so that it can be parsed by the front-end? 如何通过JSON将表数据从HTML前端和HTML前端传递到Django中的服务器端? - How to pass a table data from and HTML front end to the server side in Django through JSON? 如何从前端JavaScript发送JSON数据到使用Python搭建的后端Server Flask - How to send JSON data from front-end JavaScript to the Back-end Server built by using Python Flask 无法从后端到前端的角度选择选项获取文件列表? - Unable to fetch list of files from back-end to front-end angular select options? 如何从数组中获取 ID 列表,将每个项目与 Firestore 中的 ID 匹配,并将关联数据的 JSON 返回给前端站点? - How to take a list of IDs from an array, match each item to the ID in the firestore, and return a JSON of the associated data to front-end site?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM