我是Angular2的新手,并且我试图从输入中将新项添加到列表中。但是然后单击以提交我,而不是文本[object Object]。

这是我的代码:

app.component.html

<form (submit)="addItem(item)">
    <md-input-container>
      <input [(ngModel)]="name" mdInput placeholder="add" name="addNew">
    </md-input-container>
      <button type="submit" md-icon-button>
          <i class="material-icons">send</i>
    </button>
    </form>

app.component.ts
items = Players;
 name;

 addItem(name): void {
        this.items.push(new Player({
            name : name
        }));
    }

玩家
export class Player {
    id: number;
    name: string;
    count: number;

    constructor(name){
        this.id;
        this.name = name;
        this.count = 0;
    }

所有玩家
export let Players: Player[] = [
];

谢谢你的回答

最佳答案

我会写一个完整的答案。

在您的组件(TS文件)中:

items = Players;
name;

addItem() { this.items.push(new Player({ name : this.name })); }

在您的HTML中:
<form (submit)="addItem()" novalidate>
    <md-input-container>
        <input [(ngModel)]="name" mdInput placeholder="add" name="addNew">
    </md-input-container>
    <button type="submit" md-icon-button>
        <i class="material-icons">send</i>
    </button>
</form>

我的猜测是您提交了2次表格。

10-06 02:30