【问题标题】:passing data to html2canvas in angular2在angular2中将数据传递给html2canvas
【发布时间】:2018-06-27 13:59:15
【问题描述】:

我正在尝试在 html2canvas() 中使用变量“问题”。这个变量是一个对象数组。

我可以在 html2canvase() 的外部进行 console.log 它,但不能在内部。有没有办法传到里面?

这是在 app.component.ts 中

    download(){
    console.log("outside -> download() " + this.problems.length);//works
    html2canvas(document.getElementById('graph')).then(function(canvas) {
    console.log("inside -> download() " + this.problems.length);//not working

    var img = canvas.toDataURL("image/png");
    var doc = new jsPDF();
    ...............

    // var dnow = Date.now();
    // var d = new Date(dnow);

    doc.setTextColor(0);
    doc.text(5,5,'date here');//will get date in here
    doc.addImage(img,'JPEG',120,20);
    doc.save('testCanvas.pdf');
    });
  }

【问题讨论】:

    标签: angular jspdf html2canvas


    【解决方案1】:

    html2canvas函数内部this的作用域不同, 所以为此外部创建一个引用变量并在 html2canvas 函数中使用该引用变量,更改后的代码如下所示,

     download(){
        console.log("outside -> download() " + this.problems.length);//works
        var that = this; // assign this to variable that and use it inside html2Canvas function
        html2canvas(document.getElementById('graph')).then(function(canvas) {
        console.log("inside -> download() " + that.problems.length); // now that can be used here
    
        var img = canvas.toDataURL("image/png");
        var doc = new jsPDF();
    
        doc.setTextColor(0);
        doc.text(5,5,'date here');//will get date in here
        doc.addImage(img,'JPEG',120,20);
        doc.save('testCanvas.pdf');
        });
      }
    

    【讨论】:

      猜你喜欢
      • 2017-03-26
      • 1970-01-01
      • 2017-08-12
      • 2017-06-12
      • 2017-06-01
      • 2016-09-19
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多