C. *_*rns 73 observable rxjs typescript angular
我试图了解如何在Angular 2中使用Observables.我有这个服务:
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));
}
}
Run Code Online (Sandbox Code Playgroud)
此BehaviorSubject从另一个服务推送新值:
that._appointmentChoiceStore._appointmentChoices.next(parseObject)
Run Code Online (Sandbox Code Playgroud)
我在组件中以可观察的形式订阅它,我希望将其显示在:
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
})
}
}
Run Code Online (Sandbox Code Playgroud)
并尝试在视图中显示如下:
<li *ngFor="#appointment of _nextFourAppointments.availabilities | async">
<div class="text-left appointment-flex">{{appointment | date: 'EEE' | uppercase}}
Run Code Online (Sandbox Code Playgroud)
但是,可用性不是可观察对象的属性,因此它会出错,即使我认为我在可用性界面中定义它也是如此:
export interface Availabilities {
"availabilities": string[],
"length": number
}
Run Code Online (Sandbox Code Playgroud)
如何使用异步管道和*ngFor从可观察对象异步显示数组?我得到的错误信息是:
browser_adapter.js:77 ORIGINAL EXCEPTION: TypeError: Cannot read property 'availabilties' of undefined
Run Code Online (Sandbox Code Playgroud)
Rel*_*ros 126
这是一个例子
// 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>
Run Code Online (Sandbox Code Playgroud)
Mar*_*erg 19
谁也曾因这篇文章而跌跌撞撞。
我相信是正确的方法:
<div *ngFor="let appointment of (_nextFourAppointments | async).availabilities;">
<div>{{ appointment }}</div>
</div>
Run Code Online (Sandbox Code Playgroud)
小智 6
我想你正在寻找的是这个
<article *ngFor="let news of (news$ | async)?.articles">
<h4 class="head">{{news.title}}</h4>
<div class="desc"> {{news.description}}</div>
<footer>
{{news.author}}
</footer>
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
115161 次 |
最近记录: |