【问题标题】:invoke angular service call inside "then(function(){})"在“then(function(){})”中调用 Angular 服务调用
【发布时间】:2020-07-08 05:13:20
【问题描述】:

当用户单击按钮时,我有一个要求,我需要对该视图进行截图并将其发送回后端,将其保存为 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.
       });  
  });

我需要在里面打电话给我的服务,然后不知道你怎么能帮我解决这个问题?

谢谢,

【问题讨论】:

    标签: angular angular-promise


    【解决方案1】:

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-10-26
      • 2018-11-17
      • 2020-10-20
      • 1970-01-01
      • 2019-09-29
      • 2018-01-18
      • 2020-03-29
      • 1970-01-01
      相关资源
      最近更新 更多