模型更改後模板未更新 (Template not updating after model changed)


問題描述

模型更改後模板未更新 (Template not updating after model changed)

我遍歷一個包含對象的數組:

<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


參考解法

方法 1:

You can use filter to exclude the element you want by an object property and reassign:

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

Plunker which is minimable and verifiable: http://plnkr.co/edit/tpl:AvJOMERrnz94ekVua0u5?p=preview

方法 2:

Use 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>

Delete function:

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;
        }
    )
}

(by user5423851silentsodBendYourTaxes)

參考文件

  1. Template not updating after model changed (CC BY‑SA 2.5/3.0/4.0)

#angular2-template #Angular






相關問題

Angular2:TypeError:p 在 FIREFOX 上的 system.src.js 中未定義 (Angular2 : TypeError: p is undefined in system.src.js ON FIREFOX)

*ngIf 行為:如何有條件地以角度 2 顯示數據? (*ngIf behaviour: how to conditionally show data in angular 2?)

是否可以抽像出模板的公共部分? (Is it possible to abstract out common parts of template?)

我正在使用嵌套組件創建通用菜單,但收到錯誤消息 (I'm using nested componentes to create a generic menu but I receive an error)

Angular 2 - 在組件之間與 API 數據共享變量時未定義 (Angular 2 - undefinded when sharing variable with API data between component)

Angular 2 將動態模板解析為字符串 (Angular 2 parse dynamic template as string)

Angular 2.2.0 組件相對路徑(SystemJS)模塊未定義 (Angular 2.2.0 component-relative path (SystemJS) module is not defined)

模型更改後模板未更新 (Template not updating after model changed)

在下拉角度 2 中顯示佔位符 (Display placeholder in dropdown angular 2)

在孩子中使用結構的指令模板 (Use structural's directive template in child)

可重複使用的 Angular 11 模板 (Reusable angular 11 template)

無法將 .ts 中的數據渲染到 mat-table 或 Angular 2 中的一般 html 文件 (Unable to render data from .ts to mat-table or in general html file in angular 2)







留言討論