4
這是問題:我有一個服務,使在構造一個HTTP請求:如何注入服務與構造異步調用,角2
constructor(public http: Http, public geolocation: Geolocation) {
this.http = http;
this.geolocation = geolocation;
//Http request... this will set variable forecast of the class when complete.
this.getForecast(16);
}
然後我注射的是服務在這樣的組件:
constructor(public connector: ApiConnector) {
this.forecast = connector.forecast;
}
如果我嘗試使用組件類的預測部件上的組件裝飾,因爲我在這裏做的:
@Component({
selector: 'app',
directives: [MainForecast, DailyForecast],
template: `
<main-forecast [main-weather]="forecast"></main-forecast>
<daily-forecast [weather-list]="forecast.list"></daily-forecast>
`,
})
我得到一個錯誤,「無法讀取屬性「列表'undefined in ...「
有沒有可能在組件構造函數中使用promise?
如果'預測'是一個承諾,你應該這樣做'[weather-list] =「(forecast | async)?list」' –
它不是一個承諾/觀察本身。但好的方法我會研究和觀察如果我可以改變預測是一個承諾。 –
這很好,如果它不是一個承諾或可觀察(我認爲這是因爲你的問題的一部分)。你可以刪除'| async'並離開'forecast?.list'。請參閱[elvis操作員](https://angular.io/docs/ts/latest/guide/template-syntax.html) –