我所试图做的是在新窗口中打开局部视图,并调用打印功能,但问题是,局部视图打印功能后呈现,所以我总是得到一个空白页。我试着用 $超时
的功能,但我得到了相同的结果。现在,我有这个,但是这是一个哈克解决方案,我不喜欢它:
$ scope.print =功能(){ 的setTimeout(函数(){ 打印() },1000);}
这是网页的HTML,我尝试打开:
< DIV ID =printWrapper的风格=背景色:白色; NG-控制器=accountContentControllerNG-的init =打印()> <表格边框=0的cellpadding =0CELLSPACING =0WIDTH =100%> <! - 头文件 - > &所述; TR> &所述; TD> &所述; IMG的id =imgLogoSRC =@ Model.TicketPayOut.Logo/> < / TD> < / TR> <! - 头文件 - > &所述; TR> &所述; TD> <标签ID =lblTerminalId级=左边的位置填充-TOP-3填充左-3文本式> @ Translator.Translate(终端)LT; /标签> <跨度类=左位填充-TOP-3文本式>:< / SPAN> <标签ID =lblTerminalIdValue级=左边的位置填充-TOP-3填充左-3文本式>< /标签> < DIV的风格=明确:既>< / DIV> <标签ID =lblTerminalName级=左位填充左-3文本式> @ Translator.Translate(BET_OFFICE)< /标签> <跨度类=左文后式>:< / SPAN> <标签ID =lblTerminalNameValue级=左位填充左-3文本式>< /标签> <标签ID =lblTerminalCityAddress级=左位填充左-3文本式的风格=明确:既;>< /标签> <标签ID =lblCompanyInfo级=中心位置的文本式的风格=明确:既;>< /标签> <标签ID =lblCompanyAddress级=中心位置的文本式的风格=明确:既;>< /标签> <标签ID =lblCompanyId级=中心位置的文本式的风格=明确:既;>< /标签> < / TD> < / TR> &所述; TR> < TD类=BORDER-上边框底部> < DIV的风格=填充:10px的0;> <标签ID =lblStornoMessage级=中心位置的文本样式> @ Translator.Translate(PAYOUT_CONFIRMATION)< /标签> < / DIV> < / TD> < / TR> &所述; TR> < TD类=BORDER-底> < DIV的风格=高度:25px的;填充:10px的的3px 0的3px;> <标签ID =lblPayoutTicket级=左处的文本式> @ Translator.Translate(PAYOUT_TICKET)< /标签> <标签ID =lblPinValue级=右位置文本式> {{payoutTime |日期:日期格式}}< /标签> < / DIV> < / TD> < / TR> &所述; TR> &所述; TD> < DIV的风格=填充:5像素的3px;> <标签ID =lblPinTicket级=左处的文本式> @ Translator.Translate(PIN)< /标签> <标签ID =lblPinReturnValue级=右位置文本式> {{ticketPin}}< /标签> < / DIV> < / TD> < / TR> &所述; TR> &所述; TD> < DIV的风格=填充:5像素的3px;> <标签ID =lblPayinReturn级=左处的文本式> @ Translator.Translate(PAYOUT_AMOUNT)< /标签> <标签ID =lblPayinReturnValue级=右位置文本式> {{payoutAmount}}< /标签> < / DIV> < / TD> < / TR> &所述; TR> < TD类=BORDER-底> < DIV的风格=填充:25像素的3px 5像素的3px;> <标签ID =lblCreatedBy级=左文后式> @ Translator.Translate(CREATED_BY)< /标签> <标签ID =lblCreatedByValue级=右位置文本式> @ User.Identity.Name< /标签> < / DIV> < / TD> < / TR> < /表>< / DIV>
这是网页上的按钮,我哪里有打印选项:
< DIV CLASS =MR-10> < DIV CLASS =右拉填充-8 MT5 COL-LG-2 COL-MD-2> <输入类型=提交值=@ Translator.Translate(取消)级=BTN BTN-块辅助按钮保存的改动填充-8NG点击=CancelPayOutTicket(ticketPin)/&GT ; < / DIV> < DIV CLASS =右拉填充-8 MT5 COL-LG-2 COL-MD-2> <输入类型=提交值=@ Translator.Translate(支出)级=BTN BTN-块保存的改动填充-8 ng-class=\"{'secondary-button':TicketsPayout.BettingSli$p$psult.TicketHolder.PayoutEnabled==true,'disabled_button':TicketsPayout.BettingSli$p$psult.TicketHolder.PayoutEnabled==false}\" NG-禁用=TicketsPayout.BettingSli presult.TicketHolder.PayoutEnabled ==假NG点击=FinishTicketPayout(ticketPin); ConfirmTicketPayOut(ticketPin,@用户名')/> < / DIV> < / DIV>
有什么办法避免的setTimeout
函数并调用打印功能在新的窗口和填充局部视图的数据?
编辑:角控制器:
$ scope.CheckTicket =功能(ticketPin){ 如果(ticketPin = NULL&放大器;!&安培; ticketPin =与&!&安培;!ticketPin =未定义){ VAR承诺= accountDataProviderService.checkTicketPayout(ticketPin); $ scope.checkPromise =承诺; promise.then( 功能(响应){ $ scope.showTicketPayout =真; $ scope.TicketsPayout =响应; }, 功能(错误){ $ scope.showTicketPayout = FALSE; $ scope.ticketNotFound =真; $超时(函数() { $ scope.ticketNotFound = FALSE; },ticketNotFound * 1000); }); } } $ scope.CloseMessage =功能() { $ scope.ticketNotFound = FALSE; } $ scope.FinishTicketPayout =功能(ticketPin) { accountDataProviderService.finishTicketPayOut(ticketPin) 。然后(功能(响应){ $ scope.finishTicketPayOut =响应; localStorage.setItem(payoutTime,$ scope.finishTicketPayOut.PayoutTime); localStorage.setItem(payoutAmount,$ scope.finishTicketPayOut.PayoutAmount); }); } $ scope.ConfirmTicketPayOut =功能(ticketPin,用户名){ $ scope.ticketPin = ticketPin; localStorage.setItem(针,ticketPin); accountDataProviderService.confirmTicketPayOut(ticketPin,用户名) 。然后(功能(响应){ $ scope.confirmTicketPayOut =响应; 如果($ scope.confirmTicketPayOut.Result ==真){ VAR newWindow = window.open(/打印) } }); localStorage.clear(); }
解决方案
让数据包你的控制器里面一个承诺:
angular.module(printModule)。控制器('printController',['$范围,$窗口','$ Q',函数($范围,$窗口,$ q){$ scope.ticketPin = localStorage.getItem(针);$ scope.payoutTime = localStorage.getItem(payoutTime);$ scope.payoutAmount = localStorage.getItem(payoutAmount);变种延迟= $ q.defer();defer.resolve($ scope.ticketPin);defer.resolve($ scope.payoutTime);defer.resolve($ scope.payoutAmount);defer.promise.then(函数(){ $ window.print();})}]);
有一个愉快的一天;)
What I am trying to do is to open partial view in new window and call print function, but problem is that partial view renders after print function so I always get a blank page. I tried with $timeout
function but I get the same result. For now, I have this but this is a hacky solution and I don't like it:
$scope.print = function() {
setTimeout(function() {
print()
}, 1000);
}
This is html of page that i try to open:
<div id="printWrapper" style="background-color:white;" ng-controller="accountContentController" ng-init="print()">
<table border="0" cellpadding="0" cellspacing="0" width="100%">
<!-- HEADER -->
<tr>
<td>
<img id="imgLogo" src="@Model.TicketPayOut.Logo" />
</td>
</tr>
<!--HEADER-->
<tr>
<td>
<label id="lblTerminalId" class="left-position padding-top-3 padding-left-3 text-style">@Translator.Translate("TERMINAL")</label>
<span class="left-position padding-top-3 text-style">:</span>
<label id="lblTerminalIdValue" class="left-position padding-top-3 padding-left-3 text-style"></label>
<div style="clear:both"></div>
<label id="lblTerminalName" class="left-position padding-left-3 text-style">@Translator.Translate("BET_OFFICE")</label>
<span class="left-post text-style">:</span>
<label id="lblTerminalNameValue" class="left-position padding-left-3 text-style"></label>
<label id="lblTerminalCityAddress" class="left-position padding-left-3 text-style" style="clear:both;"></label>
<label id="lblCompanyInfo" class="center-position text-style" style="clear:both;"></label>
<label id="lblCompanyAddress" class="center-position text-style" style="clear:both;"></label>
<label id="lblCompanyId" class="center-position text-style" style="clear:both;"></label>
</td>
</tr>
<tr>
<td class="border-top border-bottom">
<div style="padding:10px 0;">
<label id="lblStornoMessage" class="center-position text-style">@Translator.Translate("PAYOUT_CONFIRMATION")</label>
</div>
</td>
</tr>
<tr>
<td class="border-bottom">
<div style="height:25px;padding:10px 3px 0 3px;">
<label id="lblPayoutTicket" class="left-position text-style">@Translator.Translate("PAYOUT_TICKET")</label>
<label id="lblPinValue" class="right-position text-style">{{payoutTime | date: dateFormat }}</label>
</div>
</td>
</tr>
<tr>
<td>
<div style="padding:5px 3px;">
<label id="lblPinTicket" class="left-position text-style">@Translator.Translate("PIN")</label>
<label id="lblPinReturnValue" class="right-position text-style">{{ticketPin}}</label>
</div>
</td>
</tr>
<tr>
<td>
<div style="padding:5px 3px;">
<label id="lblPayinReturn" class="left-position text-style">@Translator.Translate("PAYOUT_AMOUNT")</label>
<label id="lblPayinReturnValue" class="right-position text-style">{{payoutAmount}}</label>
</div>
</td>
</tr>
<tr>
<td class="border-bottom">
<div style="padding:25px 3px 5px 3px;">
<label id="lblCreatedBy" class="left-post text-style">@Translator.Translate("CREATED_BY")</label>
<label id="lblCreatedByValue" class="right-position text-style">@User.Identity.Name</label>
</div>
</td>
</tr>
</table>
</div>
This is button on page where i have print option :
<div class="mr-10">
<div class="pull-right padding-8 mt5 col-lg-2 col-md-2">
<input type="submit" value="@Translator.Translate("CANCEL")" class="btn btn-block secondary-button save-changes padding-8" ng-click="CancelPayOutTicket(ticketPin)" />
</div>
<div class="pull-right padding-8 mt5 col-lg-2 col-md-2">
<input type="submit" value="@Translator.Translate("PAYOUT")" class="btn btn-block save-changes padding-8" ng-class="{'secondary-button':TicketsPayout.BettingSlipResult.TicketHolder.PayoutEnabled==true,'disabled_button':TicketsPayout.BettingSlipResult.TicketHolder.PayoutEnabled==false}" ng-disabled="TicketsPayout.BettingSlipResult.TicketHolder.PayoutEnabled==false" ng-click="FinishTicketPayout(ticketPin);ConfirmTicketPayOut(ticketPin,'@username')"/>
</div>
</div>
Is there any way to avoid setTimeout
function and just call print function in new window and populate partial view with data?
EDIT: angular controller:
$scope.CheckTicket = function (ticketPin) {
if (ticketPin != null && ticketPin != "" && ticketPin != undefined) {
var promise = accountDataProviderService.checkTicketPayout(ticketPin);
$scope.checkPromise = promise;
promise.then(
function (response) {
$scope.showTicketPayout = true;
$scope.TicketsPayout = response;
},
function (err) {
$scope.showTicketPayout = false;
$scope.ticketNotFound = true;
$timeout(function ()
{
$scope.ticketNotFound = false;
}, ticketNotFound * 1000);
});
}
}
$scope.CloseMessage = function ()
{
$scope.ticketNotFound = false;
}
$scope.FinishTicketPayout = function (ticketPin)
{
accountDataProviderService.finishTicketPayOut(ticketPin)
.then(function (response) {
$scope.finishTicketPayOut = response;
localStorage.setItem("payoutTime", $scope.finishTicketPayOut.PayoutTime);
localStorage.setItem("payoutAmount", $scope.finishTicketPayOut.PayoutAmount);
});
}
$scope.ConfirmTicketPayOut = function (ticketPin, username) {
$scope.ticketPin = ticketPin;
localStorage.setItem("pin", ticketPin);
accountDataProviderService.confirmTicketPayOut(ticketPin, username)
.then(function (response) {
$scope.confirmTicketPayOut = response;
if ($scope.confirmTicketPayOut.Result == true) {
var newWindow = window.open("/print")
}
});
localStorage.clear();
}
解决方案
make the data "package" a promise inside your controller:
angular.module("printModule").controller('printController', ['$scope', '$window', '$q', function ($scope, $window, $q) {
$scope.ticketPin = localStorage.getItem("pin");
$scope.payoutTime = localStorage.getItem("payoutTime");
$scope.payoutAmount = localStorage.getItem("payoutAmount");
var defer = $q.defer();
defer.resolve($scope.ticketPin);
defer.resolve($scope.payoutTime);
defer.resolve($scope.payoutAmount);
defer.promise.then(function () {
$window.print();
})
}]);
have a nice day ;)