2
When clicking a button to print a report, it should consult with Angularjs some information in the database and then execute the command to print ($window.print()
) after the page is already loaded with the appropriate information.
When I try to do this, it goes to the print preview before the page is fully loaded. It is certain that simply the user wait the same load and click on print could solve but, in my case, the automatic printing would be much more interesting.
I need that command $window.print()
run only after the proper 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 verification (r.sucesso
) about the query. This code is inside the corresponding controller of the screen in specific.
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 is not very clear.
What are the missing information? is an ajax request that runs or arrives with
$(document).ready()
?– Sergio
I have tried using this command, but it did not work, because it is executed after the scripts are loaded but before Angular put the information of its call ajax on the page.
– Anderson Brunel Modolon
So you can put the
window.print()
within thesuccess(function(data, status, headers, config) {
angular.– Sergio
For this attempt the reference fields are empty and after I cancel the print, the data appears.
– Anderson Brunel Modolon
You can give a
timeout
in your print, although this does not guarantee that the page will be 100% complete at all times– Caio Felipe Pereira
Okay, it’s time to put the code you have and if possible an example of the problem working on a website or jsFiddle. Otherwise we’ll be guessing what it might be :)
– Sergio
Checking in the browser the page takes around 1.79s (value according to the browser) to load, but using window.setTimeout(Function() { window.print()}, 1); my problem is solved. I found very strange the fact of putting only 1ms of time the problem to be solved. I believe this one runs after full page loading.
– Anderson Brunel Modolon
Where are you wearing
funcionarioService.getListagem
?– Sergio
Gringo has a good discussion on this: http://stackoverflow.com/questions/21715256/angularjs-event-to-call-after-content-is-loaded
– Caio Felipe Pereira
I use this directive ng-Cloak on the body it causes the angular to wait for page loading. It tests maybe it does not need the timeout. ng-Cloak official documentation
– claudsan
@Sergio: Inside the page controller
– Anderson Brunel Modolon