Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular2: Rendering data from Observables

Following interpolation

{{ (__responseData | async)?.calculation | json }}

outputs following structure

[
    {
        "gross": 26.625834,
        "net": 20.425833,
        "tax": 6.2000003
    }
]

How can I get gross?

{{ (__responseData | async)?.calculation[0].gross }}

does not work, and the all the following trys don't work too:

{{ (__responseData.calculation[0] | async)?.gross }}
{{ (__responseData.calculation.[0].gross  | async) }}

What's the mistake?

EDIT: As workaround I use flatmap (this.__responseData.flatMap((data: any) => data.calculation);) but I would like to have an elegant solution,..


1 Answers

That's the answer from Alexander (Angular2-Github-Platform)

You could use *ngFor="let calculation of calculations | async"

@Component({
  selector: 'my-app',
  providers: [HTTP_PROVIDERS],
  template: `
    <div *ngFor="let calculation of calculations | async">
      Gross: {{ calculation.gross }}<br>
      Net: {{ calculation.net }}
    </div>
  `,
  directives: [NgFor]
})
export class App {
  calculations: Observable<{gross:number; net:number}[]>;

  constructor(private _http: Http) {
    this.calculations = this._http.get("./data.json")
                            .map(res => res.json().calculation)
  }
}

http://plnkr.co/edit/3ze70dYycQxNyXA286mX?p=preview


Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!