当“点击”按钮被选中到cards[]数组时,需要帮助推送新卡。我尝试过无数种方法,但似乎无法解决这个简单的问题。援助是非常宝贵的!
@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);
}
}
}
发布于 2017-11-04 07:02:31
你的做法是错误的,因为:
response.json
重新分配到this.cards
deal
方法被调用时,它只需订阅,然后在条件this.cards
为true
的情况下将this.cards
数组推入自身。
deal(cardCount)
{
this._data.deal(cardCount, this.deck.deck_id).subscribe(response => {
const cards = response.json()
if(cards) {
this.cards.push(cards);
}
});
发布于 2017-11-04 05:59:03
您需要将代码放入订阅中,因为它是异步的。
deal(cardCount){
this._data.deal(cardCount, this.deck.deck_id).subscribe(response =>
if(response) {
this.cards.push(response.json());
}
);
}
https://stackoverflow.com/questions/47107927
复制相似问题