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

2018-06-05 16:45:38 +08:00
 wedd

没怎么写过 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 变量?
	}
  }
}
4078 次点击
所在节点    Angular
1 条回复
yimity
2018-06-05 17:03:12 +08:00
reader.onload = (e) =>{
this.base64 = result;
}
或者
let self = this;
reader.onload =function (e) {
self.base64 = this.result;
}

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/460614

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX