Snažím sa brať dáta z pozorovateľných komponentov.
Nemôžem dostať údaje do poľa v stroji, stále dostávam objekt Observable a správu negatívov typu. V HTML však slučka správne vytlačí prichádzajúce údaje.
Služby:
@Injectable()
export class DataService {
private _dataListSource: BehaviorSubject<Data[]> = new BehaviorSubject<Data[]>([]);
constructor(private http: HttpClient) {
}
getDataList(): Observable<any> {
return this.http.get<Data[]>("/rest/data/lastDatas").map(res => {
this._dataListSource.next(res);
console.log(res);
});
}
subscribeToDatas(): Observable<Data[]> {
return this._dataListSource.asObservable().distinctUntilChanged();
}
getData(): Observable<Data[]> {
return this.http.get<Data[]>("/rest/data/lastDatas");
}
}
Trieda strojopisu:
export class DataComponent implements OnInit {
dataStatuses: Data[];
public dataList$: Observable<Data[]>;
constructor(public dataService: DataService) {
}
ngOnInit() {
this.dataList$ = this.dataService.subscribeToDatas();
this.dataService.getDataList().subscribe();
console.log("Informacja ", this.dataList$);
}
}
HTML:
<div *ngIf="dataList$ | async; let dataList; ">
<div *ngFor="let data of dataList">
{{data.id}}
</div>
</div>
odpovede:
2 pre odpoveď č. 1tvoj dataList$
vráti Observable
, to je správne. HTML stále funguje, pretože používate súbor async
rúra, ktorá za vás automaticky vykoná časť „prihlásiť sa na odber“ v jazyku HTML.
Ak chcete vidieť, čo robí dataList$
návrat, jednoducho sa prihláste na odber:
this.dataList$.subscribe(data=>{
console.log("data from dataList! ",data);
})