2014-09-06 63 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

相關問題