typescript - استفاده از یک آرایه از یک شیء Observable با ngFor و Async Pipe در Angular 2


من دارم تلاش میکنم بفهمم چگونه از Observables در Angular 2 استفاده کنم. من این سرویس را دارم:

import {Injectable, EventEmitter, ViewChild} from '@angular/core';
import {Observable} from "rxjs/Observable";
import {Subject} from "rxjs/Subject";
import {BehaviorSubject} from "rxjs/Rx";
import {Availabilities} from './availabilities-interface'

@Injectable()
export class AppointmentChoiceStore {
    public _appointmentChoices: BehaviorSubject<Availabilities> = new BehaviorSubject<Availabilities>({"availabilities": [''], "length": 0})

    constructor() {}

    getAppointments() {
        return this.asObservable(this._appointmentChoices)
    }
    asObservable(subject: Subject<any>) {
        return new Observable(fn => subject.subscribe(fn));
    }
}

این BehaviorSubject به این صورت از سرویس دیگری مقادیر جدید دریافت میکند:

that._appointmentChoiceStore._appointmentChoices.next(parseObject)

من در قالب یک Observable در کامپیوننتی که میخواهم آن را نمایش دهم، به آن اشتراک میکنم:

import {Component, OnInit, AfterViewInit} from '@angular/core'
import {AppointmentChoiceStore} from '../shared/appointment-choice-service'
import {Observable} from 'rxjs/Observable'
import {Subject} from 'rxjs/Subject'
import {BehaviorSubject} from "rxjs/Rx";
import {Availabilities} from '../shared/availabilities-interface'


declare const moment: any

@Component({
    selector: 'my-appointment-choice',
    template: require('./appointmentchoice-template.html'),
    styles: [require('./appointmentchoice-style.css')],
    pipes: [CustomPipe]
})

export class AppointmentChoiceComponent implements OnInit, AfterViewInit {
    private _nextFourAppointments: Observable<string[]>
    
    constructor(private _appointmentChoiceStore: AppointmentChoiceStore) {
        this._appointmentChoiceStore.getAppointments().subscribe(function(value) {
            this._nextFourAppointments = value
        })
    }
}

و تلاش برای نمایش در نما به این صورت:

  <li *ngFor="#appointment of _nextFourAppointments.availabilities | async">
         <div class="text-left appointment-flex">{{appointment | date: 'EEE' | uppercase}}

با این حال، availabilities هنوز یک ویژگی از شیء قابل مشاهده نیست، بنابراین خطا میدهد، حتی با این که آن را در رابط availabilities به این صورت تعریف کردهام:

export interface Availabilities {
  "availabilities": string[],
  "length": number
}

چگونه میتوانم یک آرایه را به صورت ناهمزمان از یک شیء قابل مشاهده با async pipe و *ngFor نمایش دهم؟ پیغام خطایی که دریافت میکنم این است:

browser_adapter.js:77 ORIGINAL EXCEPTION: TypeError: Cannot read property 'availabilties' of undefined

پاسخ شماره ۱

در اینجا یک نمونه است

// in the service
getVehicles(){
    return Observable.interval(2200).map(i=> [{name: 'car 1'},{name: 'car 2'}])
}

// in the controller
vehicles: Observable<Array<any>>
ngOnInit() {
    this.vehicles = this._vehicleService.getVehicles();
}

// in template
<div *ngFor='let vehicle of vehicles | async'>
    {{vehicle.name}}
</div>

پاسخ شماره ۲

هر کسی که اتفاقاً با این پست برخورد کند.

من معتقدم این روش درست است:

  <div *ngFor="let appointment of (_nextFourAppointments | async).availabilities;"> 
    <div>{{ appointment }}</div>
  </div>