繁体   English   中英

如何以角度将json api数据加载到html模板?

[英]How to load json api data to html template in angular?

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

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

组件.ts

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
        });
    }

服务.ts

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);
            })
        );
    }
}

模板.html

                <div>
                    <p>{{_table.Percentage}}</p>
                </div>

json(由 django API 提供)

{
   "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
      }
   }
}

要在 html 上显示 json 数据,您可以使用json 管道

<pre>
 {{ _table.Percentage | json }}
</pre>

暂无
暂无

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

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