如何从匿名函数中发送变量的内容

时间:2017-03-26 11:09:19

标签: angular typescript meteor

我想从函数中发送文件的名称,我可以发出输出并发送到另一个组件。我试过一个原型但我无法解决它。

 file(file: File): void {

    UploadFS.selectFiles(function(file){

        var today = new Date();
        var dd = today.getTime();
        let photo = {
            name: file.name + dd,
            size: file.size,
            type: file.type
        };
        let worker = new UploadFS.Uploader({
            store: ImagesStore,
            data: file,
            file: photo,
            onComplete(file) {
                console.log(file.name + ' has been uploaded');
                this.check = file.name;

            }
        });
        worker.start();
        this.check = photo.name;
        console.log(photo.name); //here the name of file is visible

    });

    console.log("check outside33 ",this.check); // here not: output: undefinded 
    //this.onFile.emit(this.check);

}

1 个答案:

答案 0 :(得分:1)

更改

UploadFS.selectFiles(function(file){

UploadFS.selectFiles((file)=>{

您的this未提及您的组件

或使用旧的js方式:

var self = this; //<-- assign this to self here
UploadFS.selectFiles(function(file){

    var today = new Date();
    var dd = today.getTime();
    let photo = {
        name: file.name + dd,
        size: file.size,
        type: file.type
    };
    let worker = new UploadFS.Uploader({
        store: ImagesStore,
        data: file,
        file: photo,
        onComplete(file) {
            console.log(file.name + ' has been uploaded');
            self.check = file.name;

        }
    });
    worker.start();
    self.check = photo.name; //<-- use self here
    console.log(photo.name); //here the name of file is visible

});