简体   繁体   English

在AngularJS中完全加载页面后执行函数

[英]Executing a function once the page is fully loaded in AngularJS

I would like to execute this function as soon as the page is fully loaded. 我想在页面完全加载后立即执行此功能。

vm.pointInvoice = () => {   
    if ($stateParams.property == "INVOICE") {
        vm.invoiceEdit($stateParams.checkin)
        vm.invoiceFocus = true;
        vm.receiptFocus = false;
    }
}

If I put the function as a button (just to test it), everything works perfectly 如果我把这个功能作为一个按钮(只是为了测试它),一切都很完美

<button ng-click="vm.pointInvoice()">OPEN AND POINT TO INVOICE</button>

But, no matter what I do - I simply cannot get this to execute my function automatically (when the page is fully loaded and all data/elements are available). 但是,无论我做什么 - 我都无法自动执行此功能(当页面完全加载且所有数据/元素都可用时)。

Lucky me Stack Overflow had a lot of posts about page fully loaded so I tried a whole bunch of them but none of them works, they all fire off the function while the page is still blank. 幸运的是Stack Overflow有很多关于页面满载的帖子,所以我试了很多但是没有一个可以工作,它们都是在页面仍然是空白的时候启动它。

These are some of the ones I have tried: 这些是我尝试过的一些:

$scope.$on('$viewContentLoaded', function(){
    debugger;
});

angular.element(document).ready(function () {
    debugger;
});

$scope.$on('$stateChangeSuccess', function () {
    debugger;
});

So my only idea left is to do some ugly setTimeout(function(){ vm.pointInvoice() }, 3000); 所以我唯一的想法是做一些丑陋的setTimeout(function(){ vm.pointInvoice() }, 3000); hack but to me thats a bit like giving up :-D 黑客,但对我来说有点像放弃:-D

There MUST be some way to fire off an function when the page is fully loaded.... 当页面完全加载时,必须有一些方法来启动一个功能....

You may want to use the ng-init directive: 您可能想要使用ng-init指令:

<div ng-init="init()">...</div>

Define this function is your controller: 定义此功能是您的控制器:

$scope.init = function() {
    alert("Page is fully loaded!);
}

Anyway, you can call this function directly from your controller. 无论如何,您可以直接从控制器调用此功能。 It will be call once the app and the controller are loaded. 一旦应用程序和控制器加载,它将被调用。

Did you try using the right angular event? 你尝试过使用正确的angular事件吗?

Angular < 1.6.X 角度<1.6.X

angular.element(document).ready(function () {
    alert('page loading finshed');
});

Angular >= 1.6.X 角度> = 1.6.X

angular.element(function () {
    alert('page loading finshed');
});

Have you tried something outside Angular JS? 你有没有尝试过Angular JS之外的东西?

$(document).ready(function(){ /*code*/ }); //with jQuery

document.addEventListener('DOMContentLoaded', function(){ /*code*/ });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM