V2EX = way to explore
V2EX 是一个关于分享和探索的地方
Sign Up Now
For Existing Member  Sign In
wedd
V2EX  ›  Angular

ionic3 作用域问题, 如何在 FileReader()的 onload 事件中,把读取的数据传递给作用域的变量?

  •  
  •   wedd · Jun 5, 2018 · 4957 views
    This topic created in 2883 days ago, the information mentioned may be changed or developed.

    没怎么写过 ts,对 angularjs 也是边写边查, 要实现前端选择文件并读取为 base64,请教该如何做? 本人 js 水平还停留在 jquery 时代,请不吝赐教

    伪代码:

    import { Component } from '@angular/core';
    @IonicPage()
    @Component({
      selector: 'test',
      templateUrl: 'test.html',
    })
    export class TestPage {
      base64='';	//存储 base64 字符串
      constructor(
    	public navCtrl: NavController,
    	public navParams: NavParams
      ) {};
      
      readPicFile(){
        var file_ipt=document.getElementById("file_up");
        var file = file_ipt.files[0];
        var reader = new FileReader();
    	reader.readAsDataURL(file);
    	reader.onload = function (e) {
    		base64 = this.result;	//这样是错的
            //这里该如何能修改上方的 base64 变量?
    	}
      }
    }
    
    1 replies    2018-06-05 17:03:12 +08:00
    yimity
        1
    yimity  
       Jun 5, 2018
    reader.onload = (e) =>{
    this.base64 = result;
    }
    或者
    let self = this;
    reader.onload =function (e) {
    self.base64 = this.result;
    }
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   5671 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 39ms · UTC 07:42 · PVG 15:42 · LAX 00:42 · JFK 03:42
    ♥ Do have faith in what you're doing.