* ng对象angular2

时间:2016-10-28 13:00:23

标签: angular ionic-framework ngfor

我从api获取JSON文件,

正如您所看到的,对象内部有一个名为" rates" 不幸* ngFor只能在数组上走过。 我正在寻找能够获得" Key"和"价值" 像

for(var var in obj)
例如,

访问" AUD:0.41852"

{
 "base":"BRL",s
 "date":"2016-10-27",
 "rates":{
 "AUD":0.41852,
 "BGN":0.57085,
 "CAD":0.42629,
 "CHF":0.31634,
 "CNY":2.1623,
 "CZK":7.8871,
 "DKK":2.1709,
 "GBP":0.26023,
 "HKD":2.4734,
 "HRK":2.1894,
 "HUF":90.257,
 "IDR":4156.5,
 "ILS":1.2256,
 "INR":21.311,
 "JPY":33.388,
 "KRW":364.6,
 "MXN":5.9721,
 "MYR":1.3381,
 "NOK":2.6227,
 "NZD":0.44646,
 "PHP":15.465,
 "PLN":1.2636,
 "RON":1.3146,
 "RUB":20.025,
 "SEK":2.8648,
 "SGD":0.44397,
 "THB":11.191,
 "TRY":0.98786,
 "USD":0.31893,
 "ZAR":4.4196,
 "EUR":0.29188
 }

}

这是我的代码,费率对象应该是最新的"离子项"

currency.html

<ion-header style="direction:rtl;">
  <ion-navbar>
    <ion-title>
      המרת מטבע
    </ion-title>
  </ion-navbar>
</ion-header>
<ion-content padding style="direction:rtl;">
  <ion-list>
    <ion-item>
      <ion-label>בחר מטבע:</ion-label>
      <ion-select okText="אשר" cancelText="בטל" [(ngModel)]="selectCurrencyInput">
        <ion-option value="BGN">BGN</ion-option>
        <ion-option value="BRL">BRL</ion-option>
        <ion-option value="CAD">CAD</ion-option>
        <ion-option value="CHF">CHF</ion-option>
        <ion-option value="CNY">CNY</ion-option>
        <ion-option value="CZK">CZK</ion-option>
        <ion-option value="DKK">DKK</ion-option>
        <ion-option value="GBP">GBP</ion-option>
        <ion-option value="HKD">HKD</ion-option>
        <ion-option value="HRK">HRK</ion-option>
        <ion-option value="HUF">HUF</ion-option>
        <ion-option value="IDR">IDR</ion-option>
        <ion-option value="ILS">ILS</ion-option>
        <ion-option value="INR">INR</ion-option>
        <ion-option value="JPY">JPY</ion-option>
        <ion-option value="KRW">KRW</ion-option>
        <ion-option value="MXN">MXN</ion-option>
        <ion-option value="MYR">MYR</ion-option>
        <ion-option value="NOK">NOK</ion-option>
        <ion-option value="NZD">NZD</ion-option>
        <ion-option value="PHP">PHP</ion-option>
        <ion-option value="ZAR">ZAR</ion-option>
        <ion-option value="PLN">PLN</ion-option>
        <ion-option value="RON">RON</ion-option>
        <ion-option value="RUB">RUB</ion-option>
        <ion-option value="SEK">SEK</ion-option>
        <ion-option value="SGD">SGD</ion-option>
        <ion-option value="THB">THB</ion-option>
        <ion-option value="TRY">TRY</ion-option>
        <ion-option value="USD">USD</ion-option>
      </ion-select>
    </ion-item>
    <ion-item>
      <ion-label>כמות לחישוב</ion-label>
      <ion-input #amountCurrencyInput type="text"></ion-input>
    </ion-item>
    <button ion-button (click)="calc({selectedCurrency:selectCurrencyInput,amountCurrency:amountCurrencyInput.value})">
      חשב מטבע
    </button>
  </ion-list>
  <ion-list *ngIf="load" inset>
    <h2>שער המטח נכון להיום: <span>{{results.date}}</span></h2>
    <h3>המטבע הנבחר: {{results.base}}</h3>
    <ion-item>
      <!-- Object should run here.-->
    </ion-item>
  </ion-list>
</ion-content>

currency.ts

import {Component} from '@angular/core';
import {NetworkServices} from '../../services/network';


@Component({
  selector: 'currency-page',
  templateUrl: 'currency.html',
  providers: [NetworkServices]
})
export class CurrencyPage {
  public load: any;
  public results: any;

  constructor(public networkServices: NetworkServices) {
    this.load = false;
  }

  calc(details) {
    this.networkServices.getCurrency(details).then((result) => {
      this.load = true;
      this.results = result;
    });
  }
}
你们这些人:)

3 个答案:

答案 0 :(得分:8)

* ngFor不打算用于对象,但你可以通过使用Object.keys解决这个问题,试试这个可能

<div *ngFor='let key of Object.keys(yourObject)'>
    <h3>{{yourObject[key].someProperty}}</h3>
</div>

答案 1 :(得分:2)

您可以在组件上使用提取密钥或可重用管道的方法

@Pipe({name: 'keys'})
export class KeysPipe implements PipeTransform {
  transform(value) : any {
    if(!value) {
      return null;
    }
    return Object.keys(value);
  }
}
<div *ngFor="let key of someObject | keys">{{someObject[key]}}</div>

答案 2 :(得分:0)

ngFor= "let x of results?.rates"

也应该有用。