【发布时间】:2016-07-09 02:57:04
【问题描述】:
在学习 Angular 2 时,我使用 observable 通过 API 获取一些数据。像这样:
getPosts() {
return this.http.get(this._postsUrl)
.map(res => <Post[]>res.json())
.catch(this.handleError);
}
我的帖子模型看起来是这样的:
export class Post {
constructor(
public title: string,
public content: string,
public img: string = 'test') {
}
我面临的问题是地图操作员没有对 Post 模型做任何事情。例如,我尝试为 img 值设置默认值,但在视图中 post.img 什么也不显示。我什至用另一个模型(Message[])更改了 Post[],并且行为没有改变。任何人都可以解释这种行为吗?
【问题讨论】:
-
对我来说没有意义的是为什么 Typescript 在强制转换失败或然后将泛型对象传递给需要 Post 类型的函数时不会抛出错误? Typescript 类型检查不起作用吗?泛型对象可以具有不与类型化参数对齐的随机属性,因此不会发生隐式转换,但泛型对象可以毫无问题地传递。
标签: angularjs observable