首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何在angular中将json api数据加载到html模板?

如何在angular中将json api数据加载到html模板?
EN

Stack Overflow用户
提问于 2020-09-03 22:46:29
回答 1查看 147关注 0票数 0

有人能帮我回答一个简单的问题吗(我想)。我正在尝试通过Django将表格中的json文件显示到一个Angular前端应用程序中。我可以通过控制台记录数据来查看数据,但是我似乎无法将数据放入网页。

我知道如何在HTML中显示表格。具体的问题是从API获取的对象不会出现在HTML中。

component.ts

代码语言:javascript
复制
import { AfterViewInit, ChangeDetectionStrategy, Component, OnDestroy, OnInit, ViewChild, 

ViewEncapsulation } from '@angular/core';
import  ApexChart  from 'apexcharts'
import { ApexOptions } from 'ng-apexcharts';
import { FinanceService } from 'app/modules/admin/dashboards/finance/finance.service';

@Component({
  selector: 'app-finance',
  templateUrl: './finance.component.html',
  styleUrls: ['./finance.component.scss'],
  encapsulation  : ViewEncapsulation.None,
  changeDetection: ChangeDetectionStrategy.OnPush
})
export class FinanceComponent implements OnInit {

    _stockData: any;
    _portData: any;
    _effData: any;
    _calData: any;
    _labels: any;
    _table: any;

    constructor(
        private _financeService: FinanceService,
    ){
    }

    ngOnInit(): void {
        this._financeService.getFinanceData()
        .subscribe((res: any) => {
            console.log(res);
            this._stockData = res.stocks;
            this._portData = res.port;
            this._effData = res.eff;
            this._calData = res.cal;
            this._labels = res.labels;
            this._table = res.table;
            console.log(res.table);
            this._prepareChartData();
        },
        
        (response) => {

            // Re-enable the form
        });
    }

Service.ts

代码语言:javascript
复制
import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';
import { catchError, switchMap } from 'rxjs/operators';
import { BehaviorSubject, of, Observable } from 'rxjs';
import { environment } from 'environments/environment';

@Injectable({
  providedIn: 'root'
})
export class FinanceService {

    /**
     * Constructor
     * @param {HttpClient} _httpClient
     */
    constructor(private _httpClient: HttpClient){
        
    }
    // -----------------------------------------------------------------------------------------------------
    // @ Public methods
    // -----------------------------------------------------------------------------------------------------

    /**
     * get Finance Data
     */
    getFinanceData(): Observable<any>{
        return this._httpClient.get(environment.baseURL + 'apiurls/', {}).pipe(
            switchMap((response: any) => {
                return of(response);
            })
        );
    }
}

template.html

代码语言:javascript
复制
                <div>
                    <p>{{_table.Percentage}}</p>
                </div>

json (由django API提供)

代码语言:javascript
复制
{
   "stocks":[],
   "eff":[],
   "port":[],
   "cal":[],
   "labels":[],
   "table":{
      "Percentage":{
         "AD.AS":16.1,
         "ASML.AS":15.67,
         "AAPL":68.23
      },
      "Budget":{
         "AD.AS":241.5,
         "ASML.AS":235.05,
         "AAPL":1023.45
      },
      "Closing":{
         "AD.AS":25.22,
         "ASML.AS":314.3,
         "AAPL":129.04
      },
      "Number of stocks to buy":{
         "AD.AS":10.0,
         "ASML.AS":1.0,
         "AAPL":8.0
      },
      "final":{
         "AD.AS":252.0,
         "ASML.AS":314.0,
         "AAPL":1032.0
      },
      "final allocation":{
         "AD.AS":15.77,
         "ASML.AS":19.65,
         "AAPL":64.58
      },
      "Diff":{
         "AD.AS":-0.33,
         "ASML.AS":3.98,
         "AAPL":-3.65
      }
   }
}
EN

回答 1

Stack Overflow用户

发布于 2020-09-03 23:46:26

要在html上显示json数据,可以使用json pipe

代码语言:javascript
复制
<pre>
 {{ _table.Percentage | json }}
</pre>
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/63726082

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档