【问题标题】:Angular 5, adding new row in array and save thatAngular 5,在数组中添加新行并保存
【发布时间】:2018-08-02 07:10:06
【问题描述】:

我有这样的数组:

dataTest = [{name: 'Foo'},{name: 'Bar'}]

我正在通过 matIntput 显示所有数据。单击时,我将在数组中添加新行并显示空输入。没关系,但是当我将数据更改为带有 null 的新行,例如 John 并单击保存时,我的数组中没有任何更改,它看起来像这样:

dataTest = [{name: 'Foo'},{name: 'Bar'},{name: null}]

这是我的 app.compoent.html:

<mat-dialog-content>
  <div class="flex wrap vertical-align" *ngFor="let profile of dataTest; let i = index">
    <mat-form-field class="profile-name name">
        <input matInput placeholder="Name" value="{{profile.name}}">
    </mat-form-field>
    <div class="action-group">
        <span class="add-button"(click)="add()">ADD</span>
    </div>
  </div>
</mat-dialog-content>
<mat-dialog-actions align="end">
  <button class="background-white" mat-button mat-dialog-close>CANCEL</button>
  <button class="background-primary" mat-button [mat-dialog-close]="true" cdkFocusInitial (click)="save(dataTest)">SAVE</button>
</mat-dialog-actions>

这是我的 app.compoent.ts:

private add(): void {
 const rules: any = {
    name: ''
 };
 this.dataTest.push(rules);
 console.log(this.dataTest);
};


private save(dataTest) {
 console.log(dataTest)
}

任何帮助或想法我做错了什么!

【问题讨论】:

  • 根本没有双向绑定。
  • 试试 console.log(this.dataTest)

标签: javascript arrays angular object angular5


【解决方案1】:

使用 ngModel 如下:-

<input matInput placeholder="Name" [(ngModel)]="profile.name">

如果您还没有导入 FormsModule,请不要忘记。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-12-25
    • 2017-08-31
    • 2019-05-01
    • 2016-07-16
    • 2020-07-19
    • 1970-01-01
    相关资源
    最近更新 更多