I am working on an Angular class and I need to create a method that can retrieve a value from an observable and assign it to multiple class properties. Below is a snippet of my code:
export class SomeServiceClass implements OnInit {
someProperty1: someObject;
someProperty2: someObject;
somePropertyN: someObject;
ngOnInit() {
this.someProperty1 = getValueFromObservableGenericMethod("value1")
this.someProperty2 = getValueFromObservableGenericMethod("value2")
this.somePropertyN = getValueFromObservableGenericMethod("valueN")
console.log(someProperty1); // undefined
console.log(someProperty2); // undefined
console.log(someProperty3); // undefined
}
getValueFromObservableGenericMethod(someIncomingString: string): someObject {
let localVar: someObject;
this.anotherServiceClass.subscribe(
data => {
localVar = data;
},
() => {
return localVar;;
console.log(localVar);
}
)
return localVar;
}
}
After calling the method, all my class properties are showing as undefined
. The variable localVar
is available in the complete function. Does anyone have any suggestions on how to properly return values from this function call?