【问题标题】:How to send content of variable out of anonymous function如何从匿名函数中发送变量的内容
【发布时间】:2017-08-19 01:43:49
【问题描述】:

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

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

}

【问题讨论】:

    标签: angular typescript meteor


    【解决方案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
    
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-17
      • 1970-01-01
      • 2022-11-10
      • 2013-08-16
      • 2011-10-29
      • 2012-07-10
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多