EventSpinner組件訂閱EventsService提供的圖標。提供Subject Subject的服務不會收到通知
@Component({
selector: 'event-spinner',
template: `
<div class="col-xs-5">
Test <i class="fa fa-2x" [ngClass]="{'fa-check': icon == 'ok', 'fa-spin': icon == 'loading', 'fa-spinner': icon == 'loading', 'fa-times': icon == 'error'}" id="spin"></i>
</div>
`
})
export class EventSpinner implements OnInit {
icon: string;
constructor(public eventsService: EventsService) {
}
ngOnInit(): void {
this.eventsService.icons.subscribe((icon) => {
let old = this.icon;
this.icon = icon;
console.log("this.icon = " + this.icon + " (was " + old + ")");
});
}
}
icons.next在使用@角/ http.get狀態變化( 「加載」/ 「OK」/ 「錯誤」)的web服務請求調用。然而,當這種情況發生時,i標籤的類不會被更新。任何想法?
EventsService.ts
@Injectable()
export class EventsService {
icons: Subject<string> = new Subject<string>();
constructor(public http: Http) {
}
subscribe(): Observable<Event[]> {
let url = (<any>window).__ext.API_URL + 'event/view';
let obs;
return Observable
.create((o) => {
obs = o;
obs.next();
})
.flatMap(() => {
this.icons.next("loading");
console.log("executing request");
return this.http.get(url)
})
.retryWhen(error => {
this.icons.next("error");
return error.delay(3000);
})
.map((response: Response) => {
this.icons.next("ok");
console.log("response received");
setTimeout(() => {
console.log("pushing next");
obs.next();
}, 1000);
return (<any>response.json()).map(item => {
return item;
});
});
}
}
是否在觸發事件時顯示console.log? –
什麼是'EventsService'? –
@ThierryTemplier是 – zpul