【问题标题】:Angular2 ngIf not updating after file upload (with ng2-file-upload)Angular2 ngIf在文件上传后不更新(使用ng2-file-upload)
【发布时间】:2016-09-18 20:42:50
【问题描述】:

我想让上传面板在上传完成后自动隐藏,所以我有以下代码:

@Component({
    selector: 'upload-layer',
    template: `<div *ngIf="showUploadLayer">
        <!-- ...(some inputs and buttons here)... -->
</div>`,
    directives: [FILE_UPLOAD_DIRECTIVES]
})


export class UploadPanel {

    public uploader: FileUploader = new FileUploader({ url: URL });

    showUploadLayer : boolean = false;

    constructor(private ref: ChangeDetectorRef){

        this.uploader.onCompleteAll = function(){
            this.showUploadLayer = false;
            console.log("onCompleteAll");  // this one get called correctly.
            ref.detectChanges();           // try forcing a check here but still not updated
        };
    }
}

onCompleteAll() 被调用,但具有 ngIf 条件的 div 元素仍然可见,即使我将值设置为 false。 我搜索了如何强制检查更改并添加了 detectChanges() 但它仍然无法正常工作。

【问题讨论】:

    标签: angular


    【解决方案1】:

    如果您希望this 引用当前类,请不要使用function ()

    this.uploader.onCompleteAll = function(){
    

    应该是

    this.uploader.onCompleteAll = () => {
    

    另见https://developer.mozilla.org/de/docs/Web/JavaScript/Reference/Functions/Arrow_functions

    【讨论】:

      猜你喜欢
      • 2019-01-17
      • 2020-01-07
      • 2018-05-20
      • 2020-01-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-22
      • 1970-01-01
      相关资源
      最近更新 更多