2016-11-23 116 views
0

我迭代数组包含对象:模板不更新后模型改变

<tr *ngFor="let file of files | orderBy: 'id':ascending:true | paginate: {id: 'FilePagination',itemsPerPage: 20, currentPage: p}"> 
    <th [innerHTML]="project.files.indexOf(file)+1" scope="row"></th> 
    <td><a href="{{file.uri + token}}" target="_blank"><i class="fa" 
                  [class.fa-file-audio-o]="types.audio.includes(file.type)" 
                  [class.fa-file-pdf-o]="types.document.includes(file.type)"></i>{{" 
     " + file.fullName}}</a> 
    </td> 
    <td>{{file.size}}</td> 
    <td>{{file.timestamp | timeCalc}}</td> 
    <td *ngIf="adminMode"> 
     <button type="button" (click)="deleteFile(file)" 
       class="fa fa-trash-o btn btn-link p-0" title="Löschen"></button> 
    </td> 
</tr> 

调用DELETEFILE方法:

deleteFile(file: File) { 
    this.loading = true; 
    this.fileService.deleteFile(file).subscribe(
     message => this.information = message, 
     error => { 
      this.loading = false; 
      this.errorMessage = error; 
     }, 
     () => { 
      this.files.splice(this.files.indexOf(file), 1); 
      this.loading = false; 
     } 
    ) 
} 

的订阅呼叫完成后,被删除的文件不会被删除从视图。尽管如此,它肯定会从数组中移除,因为数组中所有文件的索引都会改变。这里有两张截图显示了怪异的行为:

删除之前: beforeDelte


删除后: afterDelete

+0

该代码工作在f或我,所以这是相当头疼的人。 视图中对象的实例与正在被删除的实例完全相同,对吗? http://plnkr.co/edit/lSf7NX85HSDv4frgnXCc?p=preview – silentsod

回答

2

可以使用filter通过一个对象的属性,以排除你想要的元素并重新分配:

this.files = this.files.filter(file => file.id !== fileToRemove.id);

Plunker这是minimable和可核查: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5?p=preview

+0

我认为splice会返回被移除的元素。 wouldnt将这个元素分配给this.files? – Lelsoos

+0

虽然链接的Plunker没有任务,但你是对的!我的错误留在那里。 – silentsod

+0

嗯,我的IDE指出“参数类型不匹配参数”错误。当我尝试删除文件时,这将自动删除数组中第零个索引处的文件。 – Lelsoos

0

使用project.files

HTML

<tr *ngFor="let file of project.files | orderBy: 'id':ascending:true | paginate: {id: 'FilePagination',itemsPerPage: 20, currentPage: p};"> 
    <th [innerHTML]="project.files.indexOf(file)+1" scope="row"></th> 
    <td><a href="{{file.uri + token}}" target="_blank"><i class="fa" 
                  [class.fa-file-audio-o]="types.audio.includes(file.type)" 
                  [class.fa-file-pdf-o]="types.document.includes(file.type)"></i>{{" 
     " + file.fullName}}</a> 
    </td> 
    <td>{{file.size}}</td> 
    <td>{{file.timestamp | timeCalc}}</td> 
    <td *ngIf="adminMode"> 
     <button type="button" (click)="deleteFile(file)" 
       class="fa fa-trash-o btn btn-link p-0" title="Löschen"></button> 
    </td> 
</tr> 

删除功能

deleteFile(file: File) { 
    this.loading = true; 
    this.fileService.deleteFile(file).subscribe(
     message => this.information = message, 
     error => { 
      this.loading = false; 
      this.errorMessage = error; 
     }, 
     () => { 
      this.project.files.splice(file, 1); 
      this.loading = false; 
     } 
    ) 
} 
+0

感谢您的回答!不幸的是,这是行不通的,因为两件事情: - ngFor的索引对我来说不准确,因为分页会导致索引和文件索引之间的置换 - 您的方式仍然不会从dom中删除元素。我仍然感到恼人的0 .. – Lelsoos

+0

给一个镜头,改为project.files – bendyourtaxes

+0

不改变一件事。我标记@silentsod答案是正确的。谢谢,先生! – Lelsoos