Angular 父/子绑定不起作用,绑定目标设置为未知类型的对象

Angular 父/子绑定不起作用,绑定目标设置为未知类型的对象,angular,Angular,我正在尝试创建一个类似于这里描述的父/子绑定 我遇到的第一个问题是,事件似乎是无序的,因此绑定无法工作 第二个问题是正在调用我的子组件(commentList)上输入属性的setter,但它被设置为一个不是ContentItem类型的对象。该对象不是空的、未定义的,并且没有名为ID的属性 简而言之:父组件调用服务。在服务返回之前,调用子组件中的setter并将其设置为类型不正确的对象(contentItem)。当服务返回父对象并设置Post对象时,应该更新子对象中的绑定,但不会更新 请参阅下面

我正在尝试创建一个类似于这里描述的父/子绑定

我遇到的第一个问题是,事件似乎是无序的,因此绑定无法工作

第二个问题是正在调用我的子组件(commentList)上输入属性的setter,但它被设置为一个不是ContentItem类型的对象。该对象不是空的、未定义的,并且没有名为ID的属性

简而言之:父组件调用服务。在服务返回之前,调用子组件中的setter并将其设置为类型不正确的对象(contentItem)。当服务返回父对象并设置Post对象时,应该更新子对象中的绑定,但不会更新

请参阅下面代码中的其他注释

blogDetails(父组件)

blogDetail.html

<div>
    <h1>this is blog detail</h1>
    {{Post.Title }}
    <div [innerHTML]="[Content]"></div>
</div>
<blog-comment-list [contentItem]="Post"></blog-comment-list>
commentList.html

<div>
    <div *ngFor="let comment of Comments">
        {{comment.CommentText}}
    </div>
</div>

{{comment.CommentText}

你的生活和学习都很好。我发现,在Typescript中更新对象的方式与在c#中的工作方式不同。显然,创建对象时不会创建对象属性(这很奇怪……也许我应该将它们初始化为默认值)。我在构造函数中将ID设置为零,这样子类就知道不要尝试调用api服务

export class BlogDetail implements OnActivate {
PostRoot = this.sessionService.PostRoot;
Post: ContentItem;
private Content: string;

constructor(private sessionService: SessionService, private blogService: BlogService) {
    this.Post = new ContentItem();
    this.Post.ID = 0;  // must initialize property or it will not be created on object
    this.Content = "";
}

好吧,你生活和学习。我发现,在Typescript中更新对象的方式与在c#中的工作方式不同。显然,创建对象时不会创建对象属性(这很奇怪……也许我应该将它们初始化为默认值)。我在构造函数中将ID设置为零,这样子类就知道不要尝试调用api服务

export class BlogDetail implements OnActivate {
PostRoot = this.sessionService.PostRoot;
Post: ContentItem;
private Content: string;

constructor(private sessionService: SessionService, private blogService: BlogService) {
    this.Post = new ContentItem();
    this.Post.ID = 0;  // must initialize property or it will not be created on object
    this.Content = "";
}
export class BlogDetail implements OnActivate {
PostRoot = this.sessionService.PostRoot;
Post: ContentItem;
private Content: string;

constructor(private sessionService: SessionService, private blogService: BlogService) {
    this.Post = new ContentItem();
    this.Post.ID = 0;  // must initialize property or it will not be created on object
    this.Content = "";
}