I have a component with attributes bound to a data property.
@Component({
encapsulation: ViewEncapsulation.None,
selector: 'animal-detail-main',
styleUrls: ['animal-detail-main.component.scss'],
template: `
<div>
{{data | json}}
</div>
`
})
export class DetailMain {
data: any;
private dataSvc: MainService;
constructor(
@Inject(MainService) dataSvc: MainService,
) {
this.dataSvc = dataSvc;
}
ngOnInit() {
var fetchedData: any;
this.dataSvc.currentID$
.subscribe(
currentID => {
currentID = currentID;
this.dataSvc
.getDetail(currentID)
.map(response => response.json())
.subscribe (
data => {fetchedData = data[0]},
err => console.log('Error',err),
() => {
console.log('fetchedData',fetchedData);
setTimeout(() => {
this.data = fetchedData;
console.log("this.data",this.data);
}, 2000);
}
);
});
}
}
Basically the currentID$ service holds an ID for the current object, so that's fetched back into currentID, and then currentID is passed into the getDetail service to return the entire object for that ID.
Without the setTimeout, the function returns the object data late and it never gets assigned to this.data.
With the setTimeout, the console.log right after this.data is assigned the fetchData returned object correctly logs the returned object, but the view never updates so the data isn't displayed in the component.
I expected that the component would auto-magically refresh once the data became available, but it doesn't, and I don't know how to trigger an update of the view.