将数据传递给 angular2 中的 html2canvas

passing data to html2canvas in angular2

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

我可以 console.log 它在 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');
    });
  }

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