打印表格提交
Printing out form submission
我想打印出一个已由用户填写的表单。但是,当按下打印按钮时,只打印输入的默认值。我用的是角。有人知道如何打印出填写表单,而不必追加每个输入字段(我有很多)到doctopprint .document.write?
$rootScope.printDiv = function(divName) {
var printContents = document.getElementById(divName).innerHTML;
var docToPrint = window.open('', '_blank', 'width=700,height=400');
docToPrint.document.open();
docToPrint.document.write('<html><head><link rel="stylesheet" type="text/css"/></head><body onload="window.print()">' + printContents + '</html>');
docToPrint.document.close();
};
我不知道这是否符合你的要求,但我有这个
ng-click="window.print();"
这打开了打印屏幕的所有视图,从那里你可以打印它
在angular中,我们有$scope的概念,它位于控制器和视图之间。你能做的是,在控制器中创建一个表单模型并为每个字段设置一些默认值。现在创建一个表单,并将该模型与它绑定。每个字段都会用控制器中给定的默认值填充。现在,当您按下打印按钮时,模型将具有所有更新的值,其余将是默认值。
示例如下:
h2控制器 angular.module('mainModule', [])
.conroller('formCntrl', function($scope) {
$scope.model = {
name: 'Dummy Name', // these are some default values
age: 20,
address: 'XyZ',
profession: 'Abc'
};
$scope.submitForm = function() {
/* here you can play with $scope.model which will have all updated values and if any field is not touched then it will have default value for it which is given above */
};
});
html
<html>
<body ng-app="mainModule">
<div ng-controller="formCntrl">
<form name="regForm">
Name: <input type="text" ng-model="model.name" name="name" /><br>
Age: <input type="text" ng-model="model.age" name="name" /><br>
Address: <input type="text" ng-model="model.address" name="name" /><br>
Profession: <input type="text" ng-model="model.profession" name="name"/><br>
<input type="button" value="Print" ng-click="submitForm()" />
<form>
</div>
</body>
</html>
angular.module('mainModule', [])
.conroller('formCntrl', function($scope) {
$scope.model = {
name: 'Dummy Name', // these are some default values
age: 20,
address: 'XyZ',
profession: 'Abc'
};
$scope.submitForm = function() {
/* here you can play with $scope.model which will have all updated values and if any field is not touched then it will have default value for it which is given above */
};
});
<html>
<body ng-app="mainModule">
<div ng-controller="formCntrl">
<form name="regForm">
Name: <input type="text" ng-model="model.name" name="name" /><br>
Age: <input type="text" ng-model="model.age" name="name" /><br>
Address: <input type="text" ng-model="model.address" name="name" /><br>
Profession: <input type="text" ng-model="model.profession" name="name"/><br>
<input type="button" value="Print" ng-click="submitForm()" />
<form>
</div>
</body>
</html>
这将符合您的要求。https://dzone.com/articles/building-simple-angularjs
myApp.controller('LoginCtrl', function($scope,$window) {
$scope.print=function(){
$window.print();
}
相关文章:
- 是否可以使用动作=“;someFunction()"提交表格时
- 在提交过程中使用同步确认灯箱提交表格
- 在提交未提交表格时替换Div
- 返回假后提交表格
- NG模型在提交表格时未注册
- 提交表格前检查条款和条件
- 可以'不要在express/angular应用程序上提交表格
- return false dos't禁止提交表格
- 使用Greasemonkey提交表格
- 使用AngularJS在视图中预先填写并提交表格
- 在提交表格之前,请检查是否填写了重述2
- 提交表格后留下的表格
- 避免在按下“”时提交表格;输入“;按钮
- 在提交表格时IE8赢得了't将来自输入的属性以外的属性添加到请求中
- 为什么这个代码显示异常行为?(未提交表格)
- 为什么这不足以防止重复提交表格
- 点击链接提交表格并发送到多个电子邮件 ID
- Phonegap Camera API (iOS) 提交表格
- 验证通过后提交表格
- 提交表格后在同一页面上统计