javascript - 打印函数在部分 View 之前调用

标签 javascript angularjs

我想做的是在新窗口中打开局部 View 并调用打印函数,但问题是局部 View 在打印函数之后呈现,所以我总是得到一个空白页。我尝试使用 $timeout 函数,但我得到了相同的结果。现在,我有这个,但这是一个 hacky 解决方案,我不喜欢它:

$scope.print = function() {
    setTimeout(function() {
        print()
    }, 1000);
}

这是我尝试打开的页面的 html:

<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>

这是我有打印选项的页面上的按钮:

  <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>

有什么方法可以避免使用setTimeout 函数而只在新窗口中调用打印函数并用数据填充局部 View 吗?

编辑: 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();
    }

最佳答案

将数据“打包”为 Controller 内的 promise :

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(); 
}) 
}]);

祝你有美好的一天;)

关于javascript - 打印函数在部分 View 之前调用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31610764/

相关文章:

javascript - AngularJS。添加全局 AJAX 错误处理程序(如果尚未定义)

javascript - AngularJS 按 ASC 排序

javascript - AngularJS 用户界面路由器

javascript - AngularJS 中与后端服务通信的最佳方法是什么

javascript - 在引用变量时使用变量值

javascript - 如何从json文件中正确提取信息?

javascript - 如何通过 Azure Cosmos DB 中的存储过程查询数据库

javascript - Jquery toggleClass 切换的类,但不会多次运行动画

javascript - 使用 promise 的 $scope 问题

javascript - 正好有 3 个字符和 10 个数字的 13 个字符字母数字字符串的正则表达式