print div在angularjs中不起作用

时间:2018-05-22 13:09:38

标签: javascript html angularjs

我有两个文本框。如果我在文本框中输入任何值,如果我单击打印,则输入的值不会出现在打印页面上。只有空文本框才会出现。请帮助我如何解决这个问题。

plunkr

<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8" />
  <title>AngularJS Print Directive of html templates </title>
  <link rel="stylesheet" href="style.css" />
  <script src="https://code.angularjs.org/1.4.1/angular.js"></script>
  <script>
    var app = angular.module('myApp', []);

    app.controller('myCtrl', function($scope) {
      $scope.printToCart = function(printSectionId) {
        var innerContents = document.getElementById(printSectionId).innerHTML;
        var popupWinindow = window.open('', '_blank', 'width=600,height=700,scrollbars=no,menubar=no,toolbar=no,location=no,status=no,titlebar=no');
        popupWinindow.document.open();
        popupWinindow.document.write('<html><head><link rel="stylesheet" type="text/css" href="style.css" /></head><body onload="window.print()">' + innerContents + '</html>');
        popupWinindow.document.close();
      }
    });
  </script>
</head>

<body id="printSectionId" ng-app="myApp">
  <div ng-controller="myCtrl">
    <h1>AngularJS Print html templates</h1>
    <form novalidate>
      First Name:
      <input type="text" ng-model="firstName" class="tb8">
      <br>
      <br> Last Name:
      <input type="text" ng-model="lastName" class="tb8">
      <br>
      <br>
      <button ng-click="Submit()" class="button">Submit</button>
      <button ng-click="printToCart('printSectionId')" class="button">Print</button>
    </form>
  </div>
  <div>
    <br/>
    <br/><a href="http://www.code-sample.com/2015/07/angularjs-2-forms-validation.html" target="_blank">More About AngularJS Print...</a></div>
</body>

</html>

1 个答案:

答案 0 :(得分:3)

这是一个简单的解决方案,只需在标记中添加一个值字段即可。希望它有效。

First Name:
  <input type="text" ng-model="firstName" class="tb8" value={{firstName}}>
  <br>
  <br> Last Name:
  <input type="text" ng-model="lastName" class="tb8" value={{lastName}}>
  <br>