首页 > 解决方案 > Angular File Upload Change 事件未传递正确的 ngFor 索引

问题描述

<div formArrayName="employee" *ngFor="let document of employeeForm.get('ems').controls; let i = index">
            
<input id="file_up" type="file" (change)="upload($event,i,document.get('name').value)"/>                                           
        
                    
</div>

When Any file is selected and its change event is called it is passing the initial value of index I, not the current value? 如何在 change 事件中获取 I 的当前值被调用?

标签: angular

解决方案


在这种动态循环中,总是尝试给一个trackBy函数*ngFor

<div formArrayName="employee" *ngFor="let document of employeeForm.get('ems').controls; let i = index; trackBy: trackByFun">
            
<input id="file_up" type="file" (change)="upload($event,i,document.get('name').value)"/>                                           
        
                    
</div>

在 .ts

trackByFun(index, object){
    return index;
}

推荐阅读