是否已将参数传递给Angular中的参考模型?

时间:2018-11-21 01:00:20

标签: javascript angular

我正在从JSON对象数组进行迭代,并且想在输入字段发生更改时更新项目的值。我在函数调用中传递了应该引用的项目,但似乎模型并未随之更新。我的代码段可以在下面看到:

peopleObject = [
  {name: "Alice", address: "Tokyo", isModified: false},
  {name: "Bob", address: "Manila", isModified: false}
]
formModified(isModified): void {
  isModified = true;
}
<div *ngFor="let person of peopleObject; let i=index">
  <input [(ngModel)]="person.name" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.name}}" class="form-control">
  <input [(ngModel)]="person.address" (ngModelChange)="formModified(person.isModified)" placeholder="{{person.address}}" class="form-control">
  <button (click)="savePersonDetails(person.name)" [disabled]="!person.isModified">Save</button>
</div>

1 个答案:

答案 0 :(得分:0)

如果您要尝试做的是订阅其输入更改。您可以通过以下方式实现它:

<input [ngModel]="person.name"                             // Property Binding; Removing the event binding ()
       (ngModelChange)="person.isModified = true"   // Event Binding; directly change the person.isModified to true w/o calling the function
       [placeholder]="person.name"                         // You can also use property binding here [] if you have a dynamic value to assign
       class="form-control">

已经创建了Stackblitz link供您参考。

默认名称:爱丽丝,当我添加's'-它的isModified更改为true

Demo