2016-01-23 35 views
1

在下面的Typescript代碼中,我想調用一個需要導入的函數。我可以在類的構造函數中初始化導入,但我怎麼能在公共函數中做到這一點?如何在打字稿中公開函數中初始化Http導入?

import {Http} from "angular2/http"; 

export class AppComponent { 

    constructor(properties. 
    public http: Http 
    ) { 
     this.http.get(...){} //FUNCTION 1: this function works fine 

     this.getAll(); //this does not work as FUNCTION 2 does not work 
    } 

public getAll = function(){ //FUNCTION 2: this function does not work (cannot find name'http') 
     http.get(...){} 
    } 

public otherfunction = function(){ 
     this.getAll(); //this does not work as FUNCTION 2 does not work 
} 

回答

1

公共屬性是通過this(當前實例)參考訪問:

public getAll = function() { 
    this.http.get(...); 
}