返回字典烧瓶和角度

时间:2021-03-23 16:11:03

标签: html angular typescript ionic2

我正在使用 Flask 和 ionic(angular) 创建一个应用程序,我正在尝试返回列表的 JSON 目前我的python代码如下

def get-stocks():
# Select all stocks 
cursor.execute("""SELECT * FROM `tbl_symbol_index`""")
stocks = cursor.fetchall()
stockList = []
data = {}
for stock in stocks:
    symbol = stock[0]
    companyName = stock[1]
    stockList.append({
        "symbol": symbol,
        "companyName": companyName
    })
data = {'stocks': stockList}
print(len(data["stocks"]))
return jsonify(data)

这将返回以下字典。

data = {"stocks": [
         "Symbol": "TSLA",
         "companyName": "Tesla Inc"
          ],
          [
         "Symbol": "MSFT",
         "companyName": "Microsoft"
          ],
          [
         "Symbol": "AAPL",
         "companyName": "Apple"
          ],
}

我从我的角度调用该函数

ngOnInit() {
// Get a list of all existing stocks
const Stocks = this.initializeItems();

}

  async initializeItems(){
    this.http.get("http://127.0.0.1:5000/get-stocks").subscribe(function(data) {
    })
  }

但由于某种原因,我无法在 HTML 中循环遍历它

<ion-list>
  <ion-item *ngFor="let data of Stocks | filter:filterTerm">
    <ion-label>{{data.symbol}}</ion-label>
    <ion-label>{{data.companyName}}</ion-label>
  </ion-item>
</ion-list>

1 个答案:

答案 0 :(得分:3)

你能试试这个吗:

@Component(...)
export class Component {
 data;
 ngOnInit() {
  // Get a list of all existing stocks
  this.initializeItems();
 }
 initializeItems(){
    this.http.get("http://127.0.0.1:5000/get-stocks").subscribe(datas => {
       this.data = datas;
        console.log(this.data.stocks)
    });
  }
}