Automatic print after HTML is loaded with AngularJS

2

When you click a button to print a report, it should consult with AngularJS some information in the database and then execute the command for printing (% with%) after the page is already loaded information.

When I try to do this, it goes into the print preview before the page is fully loaded. It is true that simply waiting for the same print load and click could resolve but, in my case, automatic printing would be much more interesting.

I need the command $window.print() to run only after the appropriate information is already on the page.

How do I do this check?

---- Edited:

There is a specific service where you query the data of a particular employee and a check relative ( $window.print() ) to the query. This code is inside the corresponding screen specific controller.

funcionarioService.getListagem(self.funcionarioId, function (r) { 
        if (r.sucesso) { 
            self.funcionario = r.conteudo;

            // Estranhamente desta forma soluciona meu problema
            // Acredito que este é executado após o carregamento total da página, 
            // mas não encontrei nenhuma confirmação a respeito.
            window.setTimeout(function() {
                window.print()
            }, 1); 
        }           
    });

---- Edited [2]:

This link talks about why this proposed solution works but the explanation was not very clear.     

asked by anonymous 29.05.2015 / 14:49

1 answer

0

The answer found

window.setTimeout(function() {
    window.print()
}, 0);

This is valid because not only according to this link that I had put, but in this other response is placed:

  

Async invocations, such as those from setTimeout, do indeed generate a new callstack.

where translated would be something like setTimeout generates a new stack for asynchronous invocations, running after the last stack until then.

    
29.05.2015 / 19:01