비동기 파이프를 사용하려고하는데 데이터가 표시되지 않습니다. 나는 서버에서 오는 데이터를 로깅하고 있는데 콘솔에 문제가 없다. 그러나 어떤 이유로 저는 비동기가 작동하지 않습니다.각도 4 비동기 파이프가 작동하지 않습니다.
shift.service.ts
import { Injectable } from '@angular/core';
import { Shift } from '../shift/shift';
import { HttpClient } from '@angular/common/http';
@Injectable()
export class ShiftService {
constructor(private shift:Shift, private httpClient:HttpClient
) { this.checkShiftStatus();}
checkShiftStatus():Promise<Shift>{
this.httpClient.get<Shift>('http://localhost:8080/shift/isopen')
.subscribe((data) => {
this.shift = data;
console.log(this.shift);
},
error => {
console.error('Something went wrong while trying to check the shift!');
});
return Promise.resolve(this.shift);
}
}
shift.component.ts
import { Component, OnInit } from '@angular/core';
import { Shift } from './shift';
import { ShiftService } from '../services/shift.service';
@Component({
selector: 'app-shift',
templateUrl: './shift.component.html',
styleUrls: ['./shift.component.css']
})
export class ShiftComponent implements OnInit {
isShiftOpen:Promise<boolean>;
shiftLive:Promise<Shift>;
constructor(private shiftService: ShiftService, public shift:Shift) { }
ngOnInit() {
this.isShiftOpen = this.getShift().then(shift => this.shift.active = shift.active);
this.shiftLive = this.shiftService.checkShiftStatus();
}
getShift(){
return this.shiftService.checkShiftStatus().then(shift => this.shift = shift);
}
}
버튼도 비록 표시되지 않습니다
<md-grid-list *ngIf="isShiftOpen | async" cols="2" rowHeight="100px">
<md-grid-tile>
<button md-raised-button [disabled]='isShiftOpen' color="primary">Open Shift</button>
<button md-raised-button [disabled]='!isShiftOpen' color="warn">Close Shift</button>
</md-grid-tile>
</md-grid-list>
<md-card class="card">
<md-card-title>
Shift Stats:
</md-card-title>
<md-card-content>
</md-card-content>
</md-card>
<!-- <div *ngIf="(shiftLive | async)?.notnull; else loading"> -->
<div *ngIf="shiftLive | async">
<p> Shift status: {{ shiftLive.active }} </p>
<p> Tickets Sold: {{ shiftLive.totalNumberOfTicketsSold }} </p>
</div>
<ng-template #loading>Loading Data...</ng-template>
shift.component.html 서비스는 true 값을 제공합니다. div가 나타나게하지만 shiftLive.active 나 shiftLive.totalNumberOfTicketsSold 값은 없습니다.
checkShiftStatus(): Observable<Shift>{
return this.httpClient.get<Shift>('http://localhost:8080/shift/isopen')
}
그리고 데이터와 함께 뭔가를 할 :
제안 된 변경을 수행하고 프로그램을 실행하려고 시도했습니다. 그러나 오류를 나타내는 오류가 발생했습니다. TypeError : Object.deval [as updateRenderer] (ShiftComponent.html : 18) (Object.debugUpdateRenderer [as updateRenderer])에서 정의되지 않은 의 속성 'active'를 읽을 수 없습니다. 서비스에서 this.httpClient.get을 사용하고 있습니다 ('http : // localhost : 8080/shift/isopen'); 지금은이 코드 만 사용하십시오. 오류를 잡으려고 시도조차하지 않습니다. 어떤 제안? 감사. –
Saurin
* ngIf = "shiftLive $ async를 shiftLive로 포함시킨 후, div 섹션에서로드가 제대로 작동하기 시작했으나 서버에 대해 여러 번 http 호출을하는 것이 옳습니다. – Saurin
내가 당신을 보여줬을 때 한 번만 구독해야합니다. 관찰 가능한 스트림을 두 개의 호출로 분할 할 필요가 없습니다. 필요한 모든 정보가 동일한 통화에 있습니다. – bryan60