在 "then(function(){})" 中调用 angular 服务调用

invoke angular service call inside "then(function(){})"

我有一个要求,当用户单击按钮时,我需要截取该视图的屏幕截图并将其发送回后端,在数据库中将其另存为 pdf 并下载。我正在使用 DOM to Image angular 包,它可以截屏并将其转换为图像并可以保存为客户端系统。

public takeScreenShot(){

   domtoimage.toBlob(document.getElementById('my-node'))
      .then(function (blob) {
         window.saveAs(blob, 'my-node.png');
        //I need to make a call to my service to send this blob object to backend.
   });
}

我确实尝试在 saveAs 之后调用我的服务,但是我的服务实例在那时不存在

public takeScreenShot(){
    let serviceObj= this.service;    
    domtoimage.toBlob(document.getElementById('my-node'))
        .then(function (blob) {
        window.saveAs(blob, 'my-node.png');
        this.service.sendImage(blob).subscribe(res=>{//save as pdf to client system but this.service doesn't exist inside then.
        //I did try to return blob and assign it, it doesn't work.
       });  
  });

我需要在内部调用我的服务,然后不确定您如何帮助我?

谢谢,

您必须使用箭头函数语法才能访问 this

public takeScreenShot(){
    let serviceObj= this.service;    
    domtoimage.toBlob(document.getElementById('my-node'))
        .then((blob) => {
            window.saveAs(blob, 'my-node.png');
            this.service.sendImage(blob).subscribe(res=>{//save as pdf to client system but this.service doesn't exist inside then.
            //I did try to return blob and assign it, it doesn't work.
       });  
  });