I have this code snippet below that is not working. The call to the service at the end makes an http call that shows up in the network logs that data has been returned, but that exact data is not being used for the loop in the form valuChanges snippet. What's even more surprising is that the same call to the service is being made in the constructor of my component and that works fine.
const values = this.testService.getPosts();
this.form.valueChanges.subscribe((val) => {
const temps: testModel[];
temps = this.testService.getPosts();
cn = val['name'];
format = val['cs'];
// The console.log below mysteriously outputs data, and shows
// There is data in the array variable
console.log(temps);
if (format == 'a') {
this.testModel = temp;
} else {
for (let i = 0; i < temps.length; i++) {
// The log below prints out no data
// Also when I go ahead and debug the array is empty. Even
// the network output shows returned data
console.log(temps[i]);
}
}
});
This is how the getPosts method looks like in testService
getPosts() {
const tempModel: testModel[] = [];
this.dataSource.getPosts().subscribe(response => {
if (response) {
let value = response;
value.forEach(tp => {
const temp = new testModel();
Object.assign(temp, tp);
tempModel.push(temp);
});
}
});
return tempModel;
}