Angular2 ngIf文件上传后没有更新(使用ng2-file-upload)

时间:2016-05-21 17:28:45

标签: angular

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

@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(),但是即使我将值设置为false,仍然可以看到具有ngIf条件的div元素。 我搜索了如何强制检查更改并添加了detectChanges()但它仍然无法正常工作。

1 个答案:

答案 0 :(得分:0)

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

this.uploader.onCompleteAll = function(){

应该是

this.uploader.onCompleteAll = () => {

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