Javascript 从可观测到视图问题的返回值

Javascript 从可观测到视图问题的返回值,javascript,angular,data-binding,observable,nativescript,Javascript,Angular,Data Binding,Observable,Nativescript,我正在使用nativescript进行图像上传,但是我无法将图像base64返回到我的视图进行预览,这里的问题是我的函数没有读取全局变量 这是密码 myImg:any; pickFiles() { let options = { title: "Importer", cancelButtonText: "Annuler", actions: ["Image(s)", "Video(s)", "Audio(s)", "Fichier(s)"] }; dialogs.action(opt

我正在使用
nativescript
进行图像上传,但是我无法将图像
base64
返回到我的视图进行预览,这里的问题是我的函数没有读取全局变量

这是密码

myImg:any;
pickFiles() {
let options = {
  title: "Importer",
  cancelButtonText: "Annuler",
  actions: ["Image(s)", "Video(s)", "Audio(s)", "Fichier(s)"]
};
dialogs.action(options).then(async (result) => {
  if (result == 'Image(s)') {...}
  if (result == 'Video(s)') {...}
  if (result == 'Audio(s)') {...}
  if (result == 'Fichier(s)') {...}
  this.mdf.on("getFiles", async function (res: any) {
    let results = res.object.get('results');
    let img1 = await imageSourceModule.fromFile(results[0].file).toBase64String('jpg');
    // let img2 = await imageSourceModule.fromBase64(img1)
   this.myImg;
  });
  this.mdf.on("error", function (res) {
    let msg = res.object.get('msg');
    console.log(msg);
  });
});

我希望变量
myImg
获取
img
的新值,但当我将其绑定到视图时,它将变为null。

myImg1
您从匿名函数引用的变量将不会被引用,并且您必须在控制台上获得错误。原因是匿名函数会有它;s自己的
,不会引用组件的
。因此,您需要有一个箭头功能:

您需要修改代码,如下所示:

pickFiles() {
let options = {
  title: "Importer",
  cancelButtonText: "Annuler",
  actions: ["Image(s)", "Video(s)", "Audio(s)", "Fichier(s)"]
};
dialogs.action(options).then(async (result) => {
  if (result == 'Image(s)') {...}
  if (result == 'Video(s)') {...}
  if (result == 'Audio(s)') {...}
  if (result == 'Fichier(s)') {...}
  this.mdf.on("getFiles", async (res: any) => {
    let results = res.object.get('results');
    let img1 = await imageSourceModule.fromFile(results[0].file).toBase64String('jpg');
    // let img2 = await imageSourceModule.fromBase64(img1)
    this.myImg = img1;
  });
  this.mdf.on("error", function (res) {
    let msg = res.object.get('msg');
    console.log(msg);
  });
});

this.myImg您希望如何分配新值<代码>this.myImg=img1很可能就是您要查找的内容。此外,删除
函数
并用lambda替换它,否则
将被限定范围。是的,但他没有将其作为全局变量,当我用ctrl+单击时,它将不会返回全局变量(vs code)当我试图在构造函数中给它一个简单的字符串,并在这个函数中更改它时,它不会改变。如果它没有赋值,它的
this
是不正确的,因为它被包装在
函数
块中。您可以显示更多要使用的代码吗?
导出类NvPubComponent实现OnInit{type;objectif;submitted;myImg;mdf=new Mediafilepicker();pickFiles(){}
这是我的类OK,所以只需将
异步函数(res:any)
替换为:
(res:any)
(将其转换为lambda)并将
this.myImg
分配给
img1
。使用lambda,其中的
this
将是类的
this
(在这种情况下,组件的this)它是这样的,但它也不起作用,这很奇怪,甚至当我点击这个时也是这样。myImg不会返回到全局变量。在那里放一个控制台并尝试打印myImg。你看到了什么?它打印它,但当我用其他函数再次调用它时,它显示为空