将数据传递给angular2中的html2canvas

时间:2017-09-17 18:54:08

标签: angular jspdf html2canvas

我正在尝试在html2canvas()中使用变量'problems'。这个变量是一个对象数组。

我可以在html2canvase()的外部控制它。但不在内部。有没有办法把它传递到里面?

这是在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');
    });
  }

1 个答案:

答案 0 :(得分:2)

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