2014-09-06 78 views
4

我为我的网站创建打印页:http://vivule.ee/0/print如何检测内容何时加载?

我想发起window.print()当页面加载。我尝试了不同的方法,但都没有成功。我知道这是可能的,因为我可以监听完成的Ajax调用,但它在Angular中的工作情况如何?

我的信息路径:HTML的“angular-> PHP-> MySQL的表 - > PHP-> json-> angular-> HTML

回答

2

OK,得到这个使用承诺,$手表,$超时和setTimeout的

关于承诺的工作: http://andyshora.com/promises-angularjs-explained-as-cartoon.html

关于角$表: How do I use $scope.$watch and $scope.$apply in AngularJS?

你可以看到它的工作住在这里: http://vivule.ee/0/print

这里是我结束了(检查代码中的注释)的兽:

if($routeParams.print){ 

    //Set variable for $watch to listen 
    $scope.game = null; 

    //Start script only after angular has changed content in ng-view 
    $scope.$on('$viewContentLoaded', function(){ 

     //Listen to $scope.game variable change 
     $scope.$watch('game', function(newVal, oldVal){ 

      //Force angular not to fire script on load 
      if(newVal != oldVal) { 

       //Force script to run AFTER ng-repeat has rendered its things to the DOM 
       $timeout(function(){ 

        //And finally setTimout to wait for browser to render the custom fonts for print preview 
        setTimeout(function(){ 

         //Print document 
         window.print(); 
         //window.close(); 
        }, 100); 
       }, 0); 
      } 
     }, true); 
    }); 
} 
3

可以使用,$ viewContentLoaded 发出的每一道的ngView内容被重新装载时间。 选中此链接:https://docs.angularjs.org/api/ngRoute/directive/ngView 如果您正在加载ngView,您可以使用此事件来确保视图已完全加载。

$scope.$on('$viewContentLoaded', function(){ 
    $window.print(); 
    }); 
+0

不是100%可靠的,虽然,因为它可以发射所有指令呈现 – charlietfl 2014-09-06 17:07:17

+0

这触发之前,当“NG视图”有加载。 INSIDE ng-view加载内容时,我需要监听。这现在只创建一个没有mysql表中实际内容的打印页面。 – yodalr 2014-09-07 11:48:01

0

当诺(阿贾克斯)被解决,那么你可以设置一个标志

像 getApplication.then(功能(响应){//做一些东西 $ scope.isApplicaionLoaded = TRUE; } );

,并在UI您可以使用标志像 NG秀= “isApplicaionLoaded”

+0

虽然这并没有帮助我,使用承诺的想法,但谢谢你的建议。 – yodalr 2014-09-07 15:14:04

相关问题