2016-09-15 49 views
0

我與ionic2工作,在我的控制器的ngOnInit方法我有這樣的:使變量可見外回調函數 - Ionic2

ngOnInit() 
{ 
    let a; 
    this.myAsyncMethod(function(b)) // this is a callback of an async method defined somewhere before ngOnInit() 
    { 
     a = String(b); 
    } 
} 

我希望能夠可變a傳遞給我的模板,組件文件中的其他方法。

所以我這樣嘗試:

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 
    } 
    // if I would put this.c = a here I'll get `c` undefined because I'd be outside the callback function and `a` is not defined yet. 
} 

} 
在我 template.html

我想打印c變量做{{c}}

我怎樣才能讓c可見在這兩個template.html文件和所有其他方法我有mycomponent.ts

+0

'讓自我=這一點; '然後在方法'self.c = a'中 – taguenizy

回答