gpt4 book ai didi

angular - Angular 为 4 的 html2canvas

转载 作者:太空狗 更新时间:2023-10-29 17:31:29 25 4
gpt4 key购买 nike

我可以在 Angular 4 中使用 html2canvas 截取屏幕截图,但我需要使用 http post 调用将字符串图像发送到服务器端

Component

import { Component, OnInit, NgZone } from '@angular/core';
import { Router, ActivatedRoute, Params } from '@angular/router';
import { EventsEmitter } from '../../assets/scripts/services/eventsEmitter';
import { WindowRef } from '../../assets/scripts/services/window';
import { ImagesService } from '../images/images.component.service';
import { DomSanitizer } from '@angular/platform-browser';
import * as html2canvas from "html2canvas";


@Component({
selector: 'categories',
templateUrl: 'app/components/view/view.component.html',
styleUrls: ['app/components/view/view.component.css'],
providers: [ImagesService]
})
export class ViewComponent {



constructor(
private route: ActivatedRoute,
private router: Router,
private imagesService: ImagesService,
private eventsEmitter: EventsEmitter
private sanitizer: DomSanitizer,
private window: WindowRef) {
this.window.nativeWindow.scrollTo(0, 0);
}

ngOnInit() {
}

pdfDownload() {
html2canvas(document.body).then(function (canvas) {
var imgData = canvas.toDataURL("image/png");
document.body.appendChild(canvas);
});
}


AddImagesResource(query: any) {
this.imagesService.addCanvasResource(query)
.subscribe(response => {
this.eventsEmitter.broadcast('Success', 'Changes Saved Succesfully');
},
error => {
this.eventsEmitter.broadcast('Error', 'Error Occured');
});
}
}
<a data-html2canvas-ignore (click)="pdfDownload()">screenshot</a>

我调用的发帖服务

 addCanvasResource(body: Object): Observable<any> {
let bodyString = JSON.stringify(body);
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });

return this.http.post(this.baseUrl + 'api/v3/images/AddCanvasImage', body, options)
.map((response: Response) => {
if (response.status < 200 || response.status >= 300) {
throw new Error('This request has failed ' + response.status);
}
else {
return response;
}
});
}

我无法访问 html2canvas 中的 AddImagesResource() 函数 enter image description here

能否请您告诉我如何实现上述功能

最佳答案

在 Angular 中为 promise 提供回调时,您应该使用 arrow function ,而不是匿名函数。箭头函数正确绑定(bind)到当前上下文,因此您尝试调用的函数将可访问。

试试这个:

pdfDownload() {
html2canvas(document.body).then(canvas => {
var imgData = canvas.toDataURL("image/png");
this.AddImagesResource(imgData);
document.body.appendChild(canvas);
});
}

关于angular - Angular 为 4 的 html2canvas,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44088988/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com