이 예를 살펴 보겠습니다. 구독에서 관찰 가능한 값을 반환하려고하는데 어떻게 든 빈 값을 얻습니다. 이것은 내 문제의 스크래치입니다.
this.userService.getAll().subscribe((userCollection) => {
userCollection.data.forEach((user) => {
let data = {
id: user.id
domainName: this.userService.getDomain(user.id).subscribe(
(domain) => {
console.log(domain.name); // I can see this in console
return Observable.of(domain.name);
}
)
};
this.array.push(data);
});
});
주형:
<div *ngFor="let values of array|async"> {{ values.id }} {{ values.domainName }} //empty </div>
누구든지 이것을 도와 줄 수 있습니까? 내가 올바른 방법으로하고 있습니까?
map()
대신을 사용해야 합니다 subscribe()
.
subscribe
핸들러를 바인딩하고 subscription
나중에 사용할 수 있는 객체를 반환합니다 unsubscribe()
.
domainName: this.userService.getDomain(user.id).map(
(domain) => {
console.log(domain.name); // I can see this in console
return domain.name;
}
)
또한를 반환 할 필요가 없습니다 Observable
. 매핑은 관찰 가능 항목을 반환합니다.
이 기사는 인터넷에서 수집됩니다. 재 인쇄 할 때 출처를 알려주십시오.
침해가 발생한 경우 연락 주시기 바랍니다[email protected] 삭제
몇 마디 만하겠습니다