我正在为我的网站创建打印页面:http ://vivule.ee/0/print
我想window.print()在页面加载后启动。我尝试过不同的方法,但没有成功。我知道这是可能的,因为我可以监听已完成的 Ajax 调用,但它在 Angular 中是如何完成的呢?
我的信息路径: HTML->Angular->php->mysql table->php->json->Angular->HTML
好的,通过使用 Promise、$watch、$timeout 和 setTimeout 让它发挥作用
关于承诺: http://andyshora.com/promises-angularjs-explained-as-cartoon.html
关于 Angular $watch: 如何在 AngularJS 中使用 $scope.$watch 和 $scope.$apply?
您可以在这里看到它的实时运行: 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);
});
}
Run Code Online (Sandbox Code Playgroud)