首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >角4 ->推至阵列

角4 ->推至阵列
EN

Stack Overflow用户
提问于 2017-11-04 05:55:52
回答 2查看 6.7K关注 0票数 1

当“点击”按钮被选中到cards[]数组时,需要帮助推送新卡。我尝试过无数种方法,但似乎无法解决这个简单的问题。援助是非常宝贵的!

代码语言:javascript
运行
复制
      @Component({
      selector: 'app-root',
      templateUrl: './app.component.html',
      styleUrls: ['./app.component.css']
      })
      export class AppComponent {
      title = 'app';
      deck: any;
      cards = [];
      hand = 0;
      cardCount: number;

      constructor(private _data: DataService){
        console.log("Data Service ready to query api");
        this.cards = [];
      };


      newDeck(){
            this._data.newDeck().subscribe(res => this.deck = res.json());

      }

      deal(cardCount){

        this._data.deal(cardCount, this.deck.deck_id).subscribe(response => this.cards = response.json());
        if(this.cards) {
          this.cards.push(this.cards);
        }
      }

  }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2017-11-04 07:02:31

你的做法是错误的,因为:

  • 当您收到响应时,您将response.json重新分配到this.cards
  • 当您的deal方法被调用时,它只需订阅,然后在条件this.cardstrue的情况下将this.cards数组推入自身。

代码语言:javascript
运行
复制
deal(cardCount)
    {
            this._data.deal(cardCount, this.deck.deck_id).subscribe(response => { 
                           const cards = response.json() 
                           if(cards) {
                               this.cards.push(cards);
                           }
    });

票数 2
EN

Stack Overflow用户

发布于 2017-11-04 05:59:03

您需要将代码放入订阅中,因为它是异步的。

代码语言:javascript
运行
复制
deal(cardCount){
    this._data.deal(cardCount, this.deck.deck_id).subscribe(response => 
        if(response) {
            this.cards.push(response.json());
        }
    );
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/47107927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档