일부 서비스에는 다음 구독 기능이 있습니다.타이프 스크립트 구독 기능 외부에서 값을 얻는 방법
this.sub = this.route.params.subscribe(params => {
this.id = params['id'];
this._someService
.thisById(this.id)
.subscribe(value => {
this.valueObj = value;
});
});
괜찮은 것 같습니다. 구독 기능 이외의 다음 기능에 this.valueObj을 사용해야합니다.
private _checkOpeningHours(data: any): string {
const curDayName = this._getDayName();
const todaysOpeningData = ***this.valueObj***.openHours[curDayName];
if (!todaysOpeningData) return "ERROR!";
if (!todaysOpeningData.status) return `IT'S ${curDayName.toUpperCase()} - WE ARE CLOSED TODAY!`;
return `IT'S ${curDayName.toUpperCase()}, ${new Date().toLocaleString("en-US", { hour: '2-digit', minute: '2-digit' })} - ${this._isOpen(todaysOpeningData) ? 'WE ARE OPEN' : 'SORRY, WE ARE CLOSED'}!`;
}
private _refresh() {
this.opening = this._checkOpeningHours(***this.valueObj***.openHours[this._getDayName()]);
setTimeout(() => this._refresh(), 60 * 1000);
}
어떻게 이러한 기능은 this.valueObj 작업을 얻을 수 있나요?
나는 각도에 대해 많이 알지 못하지만 많은 의존성 주입을 사용한다는 것을 기억합니다. 각도가 값을 주입하도록 메서드에 이름이 일치하는 매개 변수를 추가 할 수 있습니까? – styfle
thisById 함수가 비동기이며 thisById가 반환되기 전에 다른 함수를 호출하는 것이 문제가 아닌지 확실합니까? – toskv
'_checkOpeningHours'와'_refresh' 함수는 어디에 정의되어 있습니까? 다른 코드와 같은 클래스에 있지 않습니까? pls는 더 많은 컨텍스트를 추가합니다. – lenny