简体   繁体   English

Angular 4-HTML中onInit的打印响应

[英]Angular 4 - printing response from onInit in html

I have ngOnInit function in .ts class, and html file associated with it. 我在.ts类中具有ngOnInit函数,并且具有与之关联的html文件。

    ngOnInit() {
//some code
}.then(response => {
          console.log(response.data.reports[0].reportStatus);
         //some other code
})

generally the method works fine, and the log prints accurate results, but I tried many things and I don't know how to pass the response(and its fields) to html file so that I can print that. 通常该方法可以正常工作,并且日志可以打印出准确的结果,但是我尝试了很多事情,但是我不知道如何将响应(及其字段)传递给html文件,以便我可以打印出来。 Thx for help! 谢谢!

Well there are a few ways. 好吧,有几种方法。 You could just have a variable. 您可能只有一个变量。 For example: 例如:

yourVariable: string;

ngOnInit() {
//some code
}.then(response => {
          console.log(response.data.reports[0].reportStatus);
         this.yourVariable = response.data.reports[0].reportStatus;
})

Then in your HTML: 然后在您的HTML中:

<p>My data is: {{ yourVariable }}</p>

This is the very basics of Angular. 这是Angular的基本知识。 You should at least read the documentation before asking questions. 提出问题之前,至少应阅读文档。

.then(response => {
   console.log(response.data.reports[0].reportStatus);
   this.status = response.data.reports[0].reportStatus;
   //some other code
})

// HTML

<span>{{ status }}</span>

Initialize a variable dataToDisplay to which you will assign the result from the Promise. 初始化变量dataToDisplay ,将从Promise中分配结果。

  dataToDisplay;  
      ngOnInit() {
    //some code
    }.then(response => {
              console.log(response.data.reports[0].reportStatus);
             this.dataToDisplay = response.data.reports[0].reportStatus;
    })

And in the HTML just bind the dataToDisplay property like this 然后在HTML中像这样绑定dataToDisplay属性

<h1 *ngIf="dataToDisplay"> {{dataToDisplay}} </h1>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM