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) –