【问题标题】:How force page to wait for Angular. html2pdf.it does not render AngularJS如何强制页面等待 Angular。 html2pdf.it 不呈现 AngularJS
【发布时间】:2016-04-07 15:27:18
【问题描述】:

我想将 HTML 页面转换为 PDF。 html2pdf.it 似乎是一个不错的工具,因为it's open source 我可以将它安装在我自己的服务器上。

只需将url添加到路径:

http://www.html2pdf.it/?url=

我做了两个简单的页面。一种使用 angularjs,另一种仅使用 JavaScript。在第一个 Angular 未渲染时,我看到 {{text}}。这很奇怪,因为angularjs.org 已正确转换为 PDF。 JS版本也可以。

如何强制它等待 Angular 完成?

代码:

<html lang="en">
<head>
    <title>Minimum AngularJS page</title>
    <meta charset="UTF-8">
</head>
<body ng-app="demoApp">
    <div ng-controller="MainCtrl">
  <h1>Hello World!</h1>
          <p>{{text}}</p>
    </div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
    <script>
        angular
            .module('demoApp', [])
            .controller('MainCtrl', function($scope){
                $scope.text = "If you see this means angularjs is working";
            });
    </script>
</body>
</html>

2016 年 4 月 8 日编辑:

我不使用异步调用,例如; setTimeout()

【问题讨论】:

    标签: javascript angularjs asynchronous pdf-generation html2pdf


    【解决方案1】:

    我建议您尝试使用 ng-bind-html 从范围变量中呈现 html。类似于附加的sn-p。您必须包含 angular-sanitize.js 并包含一个模块 ngSanitize,如代码 sn-p 中所做的那样。

    <html lang="en">
    <head>
    	<title>Minimum AngularJS page</title>
    	<meta charset="UTF-8">
    </head>
    <body ng-app="demoApp">
    	<div ng-controller="MainCtrl">
      <h1>Hello World!</h1>
    		  <p ng-bind-html="text"></p>
    	</div>
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular.min.js"></script>
    <script 
    src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.3/angular-sanitize.js"></script> 
    	<script>
    		angular
    			.module('demoApp', ['ngSanitize']) 
    			.controller('MainCtrl', function($scope){
    				$scope.text = "If you see this means angularjs is working";
    			});
    	</script>
    </body>
    </html>

    谢谢, 帕拉斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-12
      • 2021-10-13
      相关资源
      最近更新 更多