As I work on developing an application, I aim to keep my component code concise and devoid of unnecessary clutter. To achieve this, I plan to offload complex logic into a service which will then be injected into the component. Suppose my component includes a variable called 'result':
my.component.ts
.....
private result:number;
Meanwhile, my service houses an asynchronous method:
my.services.ts
......
......
getNumberFromApi(){
callAsync().subscribe( data => {
console.log('This is the data obtained..'+data);
})
I wish for the ability to pass a parameter to the getNumberFromApi() method:
getNumberFromAPI(destinationVar:number){
callAsync().subscribe( data => {
console.log('This is the data obtained..'+data);
destinationVar=data;
})
This would enable me to invoke the service's method from within the component as follows:
my.component.ts
.....
private result:number;
myServiceInstance.getNumberFromApi(result);
While I am aware that directly modifying component variables from async methods may not be feasible due to stack limitations, I remain eager to explore alternatives. Is there a way in Typescript/Angular to effortlessly update component variables from services without adding extraneous lines of code? Your insights are greatly appreciated. Thank you.