鍍金池/ 問(wèn)答/HTML5  網(wǎng)絡(luò)安全  HTML/ ionic3 作用域問(wèn)題, 如何在 FileReader()的 onload 事

ionic3 作用域問(wèn)題, 如何在 FileReader()的 onload 事件中,把讀取的數(shù)據(jù)傳遞給作用域的變量?

沒怎么寫過(guò) ts,對(duì) angularjs 也是邊寫邊查, 要實(shí)現(xiàn)前端選擇文件并讀取為 base64,請(qǐng)教該如何做? 本人 js 水平還停留在 jquery 時(shí)代,請(qǐng)不吝賜教

偽代碼:

import { Component } from '@angular/core';
@IonicPage()
@Component({
  selector: 'test',
  templateUrl: 'test.html',
})
export class TestPage {
  base64='';    //存儲(chǔ) 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;    //這樣是錯(cuò)的
        //這里該如何能修改上方的 base64 變量?
    }
  }
}
回答
編輯回答
無(wú)標(biāo)題

兩個(gè)方法:

1.
readPicFile(){
  let that=this;
  ...
  reader.onload = function (e) {
       that.base64 = xxx;   
    }
  }

2.
readPicFile(){
  ...
  reader.onload = (e)=> {
       this.base64 = xxx;   
    }
  }
2017年7月22日 11:41