When working with ionic2, I encountered a situation where I needed to pass a variable from an asynchronous method to my template and other methods within the component file. In the `ngOnInit` method of my controller, I have the following code:
ngOnInit()
{
let a;
this.myAsyncMethod(function(b))
{
a = String(b);
}
}
I attempted to assign the value of `a` to a variable `c` and encountered an error:
export class myComponent {
c:string;
constructor(....){ // some stuff }
... other methods
ngOnInit()
{
let a;
this.myAsyncMethod(function(b))
{
a = String(b);
this.c = a; // here I get this error in red: EXCEPTION: TypeError: Cannot set property 'c' of undefined
}
}
}
In my `template.html`, I wanted to display the value of variable `c` using `{{c}}`. However, I struggled to make `c` visible both in the template file and all other methods within `mycomponent.ts`. How can I achieve this?