I have an array that is created and populated in a component called "player-selection". In another component called "game", I would like to access the array and add pull data from it. The array is made up of objects. Each object represents a "player". Each player has a name, stored as the playerName property. I would like to access this property so that I can display it on game.component.html. I've followed online articles to the best of my ability but something still seems to be wrong. Here is the code:
player-selection.component.ts
@Component({
selector: "app-player-selection",
templateUrl: "./player-selection.component.html",
styleUrls: ["./player-selection.component.css"]
})
export class PlayerSelectionComponent implements OnInit {
players: PlayerObject[];
constructor() {}
ngOnInit() {
this.players = [];
}
addNewPlayer() {
var element = <HTMLInputElement>document.getElementById("nameInput");
var nameInput = (<HTMLInputElement>document.getElementById("nameInput"))
.value;
var playerObject = new PlayerObject(nameInput);
this.players.push(playerObject);
element.value = "";
console.log(this.players[0].playerName);
}
}
player-selection.component.html
...</div>
</div>
<app-game [playerArr]="players"></app-game>
<router-outlet></router-outlet>
game.component.ts
@Component({
selector: "app-game",
templateUrl: "./game.component.html",
styleUrls: ["./game.component.css"]
})
export class GameComponent implements OnInit {
@Input() playerArr: PlayerObject[];
constructor() {}
ngOnInit() {
}
}
game.component.html
<mat-tab label="1">
<div *ngFor="let name of playerArr">
<h3>{{ name.playerName }}</h3>
<mat-form-field class="scoreInput">
<input matInput placeholder="Score" id="player0" />
</mat-form-field>
</div>
</mat-tab>