首页 > 解决方案 > 将对象更新为新对象的实例

问题描述

在 Angular/typescript 中,是否可以将列表中的对象实例更新为新对象,并将更改显示在 .html 上的绑定列表中?

  1. 我得到一副纸牌。
  2. 我画了三张卡片(以 HTML 显示)
  3. 绘制一张卡片并将列表中的第一张卡片更新为已绘制的新卡片,但它没有在 HTML 中更新,即使我查看数据已更新的对象也是如此。

代码:

// this works
this.Items[0] = this.Items[1]

//this does NOT work.
let newItem = new Item();
newItem.Id = x;
this.Items[0] = newItem; 


draw(deckId: string, count: number): Observable<Deck> {
  return this.http.get<Deck>(url).pipe(catchError(this.handleError));
}

drawThreeCards() {
   this.deckService.draw(this.deck.deck_id, 3).subscribe(data => {
   this.cards = data;
}

drawOneCards() {
    this.deckService.draw(this.deck.deck_id, 1).subscribe(data => {
    // Just as test I want to set the first card in the current
    // this.cards to the new card drawn

    let match = this.cards.find(x => x.Id == this.Items[0].code);
    match = this.data;
 }

标签: javascriptangulartypescript

解决方案


推荐阅读