The print function is being triggered before the partial view

My goal is to open a partial view in a new window and call the print function, but I'm facing an issue where the partial view renders after the print function, resulting in a blank page. I attempted using the $timeout function, but encountered the same problem. Currently, my workaround is as follows:

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

Here is the HTML of the page I am trying 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 button on the page includes the 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>

I'm seeking a way to eliminate the use of the setTimeout function and directly call the print function in a new window while populating the partial view with data.

Edit: Angular controller code snippet:

$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);
         });

        }
... (continued)

Answer №1

Transform the data "package" into a promise within 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(); 
}) 
}]);

Wishing you a wonderful day :)

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

How can I ensure my AngularJS Controller variable is accessible across all page contexts?

Working with AngularJS, I have a view controller where I initialize a variable called recipesData. Here is the code for the controller: (function() { 'use strict'; angular .module('myApp') .controller('Coo ...

ASP.NET "Data" Error: Trouble Parsing JSON Data on the Front-End

I am currently facing an issue with the configurations on my asmx page. The code is set up like this: using System; using System.Collections.Generic; using System.Data; using System.Linq; using System.Web; using System.Web.Script.Serialization; using Syst ...

What is preventing me from successfully retrieving data at times when using curl_setopt and jQuery Ajax?

In my quest to fetch data from another server, I am using the curl_setopt PHP function along with Ajax. The function I have created seems to be working fine, but sometimes when I refresh the page and the internet connection is slow, I don't receive an ...

Tips for expanding a fabric canvas to match the entire width of its parent division

specific scenario I have a cloth canvas placed inside a main section. How can I expand the canvas to cover the entire width and height of its container? Here is what my current code looks like: <div class="design_editor_div"> &l ...

Utilizing Bootstrap 5 for Angular Projects

With the release of version 5, the Bootstrap framework has eliminated the need for jQuery. I am curious about how this change impacts Angular applications. Can we still utilize all of Bootstrap's features in Angular, such as dropdowns? In the past, ...

Problem encountered with @HostListener

In an Angular component, I have the following code snippet that is functioning as intended: @HostListener('document:click', ['$event']) onClick(event) { if(!this.eRef.nativeElement.contains(event.target)) { console.log("clicked out ...

The ExpressJS EJS issue arises when trying to access a property that is undefined

I'm new to NodeJS and seeking assistance. I am working on a website where users can promote their virtual conferences for others to see. I have set up a form with the POST method, where the data gets pushed into an array and then displayed in an EJS f ...

How do you populate a dropdownlistfor in ASP.NET MVC after a form

My issue is that <form> @Html.DropDownListFor(x => x.City, provinces, "--Select City--", new { @class = "dropdownList" }) @Html.DropDownListFor(x => x.district, Enumerable.Empty<SelectListItem>(), "--Select district--") < ...

Typescript-powered React component for controlling flow in applications

Utilizing a Control flow component in React allows for rendering based on conditions: The component will display its children if the condition evaluates to true, If the condition is false, it will render null or a specified fallback element. Description ...

I am in the process of transforming my basic JS Array into one that contains key/value

Currently, I am utilizing jQuery to create an Array in the following manner: var arr = new Array(); $('#some-form .some-input').each(function() { arr.push($(this).val()); ...

How can you retrieve the chosen option without displaying the description of other select values in AngularJS?

How can I modify my Angular drop down so that only the main values (cause) are selected, leaving out the descriptions in the dropdown? https://i.stack.imgur.com/nSVbs.png angular.module('ui.bootstrap.demo', ['ngAnimate', 'ngSan ...

Gradually returning to the top with Vuetify's smooth scroll feature

Looking for a way to make the scrolling back to the top of the page smoother on my website. The button I currently have in place does the job, but it's not as smooth as I would like. Any suggestions on how to improve this? I've checked similar q ...

What is the best way to spin an element around its center?

I'm faced with a challenge where I have an element that needs to be rotated using JavaScript. The rotation is currently functional, but it's rotating around points other than its center. My goal is to rotate the element around its own center. ...

ES6 AngularJS 1: The Power of $inject and Inheritance

I am facing a situation where I have 3 child controllers that extend a shared parent controller. The structure looks like this: class ParentController { constructor( $scope, $state ){ this.$scope = $scope; this.$state = $state; } } ...

Exploring the file attributes within nw.js

I'm in the process of developing a native application using nw.js. I have included the following code snippet: <input id="fileDialog" type="file" accept=".pdf,.epub" multiple/><a id="add" href="#">Add</a> Below is my JavaScript cod ...

Extract data from THREE.js computed textures using GPUComputationRenderer

Experimenting with the GPUComputationRenderer on a customized version of this three.js example, I have been working on adjusting boid interactions using GPU shaders to handle, retrieve, and manipulate boid position and velocity data. I have reached a poin ...

Express is unable to locate the specified property

Here is my controller code snippet: exports.showit = function(req, res){ res.render('showpost', { title: req.post.title, post: req.post }) } In my post model, I have included title and name objects: title: {type : String, default : &apos ...

Is it important to minify JavaScript npm packages?

In my journey of creating numerous npm packages, I find myself pondering over the question: "Should JavaScript npm packages be minified?" I have always held the belief that minifying already minified code is not a good practice, which is why I have refrai ...

Can an identification be included in a label element?

My inquiry is as follows: <label for="gender" class="error">Choose</label> I am interested in dynamically adding an id attribute to the above line using jQuery or JavaScript, resulting in the following html: <label for="gender" class="err ...

Utilize styled-components in next.js to import and resize .svg files seamlessly

I have been attempting to import .svg files into my next.js project, but I have not had any success. I tried importing the .svg files the same way as in my React project by creating a typing.d.ts file and importing the svg as a component. However, it did ...