在“ then(function(){})”内调用角度服务调用

时间:2020-07-08 05:13:20

标签: angular angular-promise

当用户单击按钮时,我有一个要求,我需要对该视图进行截图并发送回后端,将其另存为pdf在DB中并下载。我正在使用DOM到Image的角度包,它能够拍摄屏幕截图并将其转换为图片,并且可以保存为客户端系统。

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.
       });  
  });

我需要打电话给我的服务部,然后不确定是否可以在这方面给我帮助?

谢谢

1 个答案:

答案 0 :(得分:2)

您必须使用箭头函数语法才能访问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.
       });  
  });