简体   繁体   English

删除数组项后,Angular 2模型绑定不正确

[英]Angular 2 model binding incorrect after removing array item

I'm having an issue where the Angular 2 model binding seems to be retaining an old index after removing and re-adding an array value. 我遇到一个问题,在删除并重新添加数组值后,Angular 2模型绑定似乎保留了旧索引。 This issue only occurs if the removed value is an index in the start or middle of the array leading me to think Angular is still retaining the old index after it's removed. 仅当删除的值是数组开头或中间的索引时,才会出现此问题,这使我认为Angular在删除后仍保留旧索引。 If I remove an item from the end of the array, and add a new one, it works as expected. 如果我从数组末尾删除一个项目,然后添加一个新项目,则它将按预期工作。

Here is my HTML: 这是我的HTML:

 <div class="row text-center" *ngFor="let date of vm.dates;let i = index;"><!--trackBy:i;--> <div class="col-md-6 text-right"> <label>Date:</label> </div> <div class="col-md-6 text-left"> <input name="Date{{i}}" type="text" class="form-control no-wrap" [(ngModel)]="date.Date" value="{{date.Date}}"> <label *ngIf="showRemoveDate(i)" (click)="removeDate(i)" class="btn btn-danger no-wrap">Remove Date</label> <label>{{date.Date}}-{{i}}</label> </div> </div> <div class="row text-center"> <div class="col-md-6 text-right"> &nbsp; </div> <div class="col-md-6 text-left"> <label (click)="addDate()" class="btn btn-danger">Add Date</label> </div> </div> 

Here is my TypeScript Code: 这是我的TypeScript代码:

    removeDate(index: number) {
    this.vm.dates.splice(index, 1);
}

addDate() {
    var date: IDates = {
        Id: "",
        Date: "05/15/2014"
    };
    this.vm.dates.push(date);
}

-If I delete the second item in a three item array, and add a new item, the text box for the second array value is the same as new item added, but my resulting view returns the correct model data. -如果删除三项数组中的第二项,然后添加一个新项,则第二个数组值的文本框与添加的新项相同,但是我得到的视图将返回正确的模型数据。

See image: enter image description here 查看图片: 在此处输入图片说明

I managed to reproduce the issue after realizing you are probably using a form. 在意识到您可能正在使用表格后,我设法重现了该问题。 You had some sort of attempt on trackBy , use that and it should solve your issue. 您对trackBy进行了某种尝试,使用它,它应该可以解决您的问题。

<form #myForm="ngForm">
  <div *ngFor="let date of vm.dates;let i = index; trackBy: trackByFn">
    <!-- more code here -->
  </div>
</form>

And the function: 和功能:

trackByFn(index: any, date: any) {
  return index;
}

Hope this helps! 希望这可以帮助! :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 数据绑定到Angular中的数组的特定项 - Data Binding to a specific item of an array in Angular 将模型绑定到文本框后调用事件-Angular - Call a event after binding model to textbox - Angular 从列表中以角度删除项目后显示通知 - Show notification after removing an item from the list in angular 角度:自动填充字段会在删除项目后更改焦点 - Angular: autocomplete field changes focus after removing an item Angular 2子组件从父组件数组中删除项目 - Angular 2 Child Component removing Item from Parent Component Array angular 多 select 框(从数组对象中删除和选择项目) - angular multi select box (removing and selecting item) from an array objects 从数组中删除项目后,它会删除所有重复条目 - Redux - After removing an item from an array it removes all duplicates entries - Redux 删除项目后更新对象数组的状态-React - Update the state of array of objects after removing item - React AngularJS:按索引删除数组项后,索引不更新 - AngularJS : index not updating after removing array item(s) by index 反应:在事件(或计时器)之后删除 useState 数组中的第一项 - React: Removing first item in useState array after event (or timer)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM