完成noob寻求帮助, 我一直在学习Angular2并尝试制作一个基本的应用程序,但现在已经破了!
component.ts文件
import {Component, OnInit} from '@angular/core';
import { Player } from './player.model';
@Component({
selector : 'app-player',
templateUrl : '/player.component.html'
})
export class PlayerComponent implements OnInit {
players: Player[] = [
new Player('Malus', 'Lina', 'lol', 3000)
];
constructor() {}
ngOnInit() {}
}
model.ts
export class Player {
public playerName: string;
public favHero: string;
public heroImage: string;
public mmr: number;
constructor( name: string, favHero: string, heroImage: string, mmr: number) {
this.playerName = name;
this.favHero = favHero;
this.heroImage = heroImage;
this.mmr = mmr;
}
}
最后错误在HTML
中我正在尝试使用{{ players.playerName }}
等但是它们尚未解决?我想这就是为什么我的应用程序现在已经破解了
答案 0 :(得分:1)
您正在尝试访问数组的对象键(players.playerName)
,这是您无法做到的。您需要访问特定索引players[0].playerName
或循环播放器并创建DOM阻止每个玩家
<div *ngFor="let player of players">
{{player.playerName}}
</div>