【发布时间】:2018-06-18 21:47:28
【问题描述】:
我是 angular4 的新手,我在显示一些信息时遇到了一些问题。
我有一个角度服务,它向我的网络服务发出 HTTP 请求:
@Injectable()
export class AstroService {
constructor(private _http: Http) {}
get() {
return this._http.get('api/astro/get')
.map((res: Response) => {
return res.json();
});
}
}
在我的组件中,我查询服务:
export class DisplayComponent {
horoscopes: any = null;
constructor(private astroService:AstroService) {
this.astroService.get()
.subscribe(data => {
console.log(data);
this.horoscopes = data;
});
}
}
但在我的组件看来,当我尝试显示它时:
<div style="border: 1px solid red">display Component</div>
<ul>
<li *ngFor="let horosocope of horoscopes">
{{horoscope.sign.name}}
</li>
</ul>
我得到了我的 12 个要点,但没有别的,Chrome 控制台告诉我它与“签名”无关:
VM4824 DisplayComponent.ngfactory.js:8 ERROR TypeError: Cannot read property 'sign' of undefined
at Object.eval [as updateRenderer] (VM4824 DisplayComponent.ngfactory.js:11)
at Object.debugUpdateRenderer [as updateRenderer] (core.js:14727)
at checkAndUpdateView (core.js:13841)
at callViewAction (core.js:14187)
at execEmbeddedViewsAction (core.js:14145)
at checkAndUpdateView (core.js:13837)
at callViewAction (core.js:14187)
at execComponentViewsAction (core.js:14119)
at checkAndUpdateView (core.js:13842)
at callViewAction (core.js:14187)
我做错了什么?抱歉,我觉得这是一个愚蠢的问题,但我无法弄清楚我错过了什么。
【问题讨论】:
-
写
<li *ngFor="let horosocope of horoscopes">{{horoscope | json}}</li>看到什么? -
几乎一样的东西,12个空
<li>
标签: angular typescript angular-components angular-template