Angular 涉及其他可观察事物的可观察事物

Angular 涉及其他可观察事物的可观察事物,angular,rxjs,observable,Angular,Rxjs,Observable,我有一个问题,我的一个可观察对象依赖于另一个可观察对象,但当父可观察对象更新时,子可观察对象似乎没有更新 客户端服务(父级) 评论服务(儿童) 并使用以下命令显示结果: *ngIf="this.comments$ | async as comments ..." 如果我调用addComment(),数组会更新,但是,当我向client.comments添加注释时,数组不会更新。是否有办法通知订阅者更改为客户端。注释??我可能遗漏了一些内容,但ClientService中没有可添加到Behavi

我有一个问题,我的一个可观察对象依赖于另一个可观察对象,但当父可观察对象更新时,子可观察对象似乎没有更新

客户端服务(父级)

评论服务(儿童)

并使用以下命令显示结果:

*ngIf="this.comments$ | async as comments ..."

如果我调用
addComment()
,数组会更新,但是,当我向
client.comments
添加注释时,数组不会更新。是否有办法通知订阅者更改为
客户端。注释?

我可能遗漏了一些内容,但ClientService中没有可添加到BehaviorSubject的内容

@Injectable()
导出类客户端服务{
私有客户端=新行为主体(“”);
clientStream$=this.client.asObservable();
}
}
变量
client
是私有的,因此不能在类外访问。
clientStream$
asObservable()
,所以你不能
。下一步(newVale)
给它新的值(至少我认为这就是asObservable的意义)。
您只粘贴了ClientService的一部分吗?-有两个关闭的科利,但只有一个打开的科利

可能会将此方法添加到ClientService

addClient(newClient){
client.next(newClient);
}

我可能遗漏了一些内容,但ClientService中没有可添加到BehaviorSubject的内容

@Injectable()
导出类客户端服务{
私有客户端=新行为主体(“”);
clientStream$=this.client.asObservable();
}
}
变量
client
是私有的,因此不能在类外访问。
clientStream$
asObservable()
,所以你不能
。下一步(newVale)
给它新的值(至少我认为这就是asObservable的意义)。
您只粘贴了ClientService的一部分吗?-有两个关闭的科利,但只有一个打开的科利

可能会将此方法添加到ClientService

addClient(newClient){
client.next(newClient);
}

您的服务是如何提供的?它们都是单例服务吗(应用程序范围)?是的,它们都是。他们应该是别的吗?不,单身应该是好的。当我向client.comments添加注释时,是否可以将您的代码添加到ClientService for
。注释,数组未更新
您的服务是如何提供的?它们都是单例服务吗(应用程序范围)?是的,它们都是。他们应该是别的吗?不,单身应该是好的。当我向client.comments添加注释时,是否可以将您的代码添加到ClientService for
。注释,数组未更新
@Injectable()
export class CommentsProvider {
  private client$: Observable<any>;
  private comments = new BehaviorSubject([]);
  comments$: Observable<any[]>

  constructor(private clientService: ClientService) {
    this.initializeComments()
  }

  initializeComments() {
    this.client$ = this.clientService.clientStream$
    this.comments$ = this.comments.asObservable()

    this.client$.subscribe( client => {
      for ( let comment of client.comments ) {
        this.addComment(comment)
      }
    })
  }

  addComment(comment) {
    let commentArray = _.cloneDeep(this.comments.getValue());
    commentArray.push(comment)
    this.comments.next(commentArray)
  }

  getComments() : Observable<any[]> {
    return this.comments$
  }
}
`this.comments$ = this.commentService.getComments();` 
*ngIf="this.comments$ | async as comments ..."