2017-11-04 3 views
0

Benötigen Sie Unterstützung beim Drücken neuer Karten, wenn der "Hit" -Knopf für das Karten [] Array ausgewählt ist. Ich habe eine Unzahl von Methoden ausprobiert, kann aber dieses einfache Problem nicht erkennen. Hilfe wird sehr geschätzt!Angular 4 -> Drücken Sie zum Array

 @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); 
     } 
     } 

    } 

Antwort

1

Ihr Ansatz ist falsch, denn:

  • Wenn Sie Antwort erhalten Sie response.json-this.cards
  • neu zuweisen
  • Wenn Ihre deal Methode aufgerufen wird, abonniert es einfach und drückt dann this.cards Array in sich selbst, wenn die Bedingung this.cardstrue ist.

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

0

Sie müssen den Code in abonnieren setzen, wie es asynchron ist

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

Hey! Ich versuchte das auch, aber ich erhalte Fehler -> FEHLER TypeError: _this.cards.push ist keine Funktion –

+0

Hat irgendeinen der oben genannten Code geändert? – Dhyey

Verwandte Themen