简体   繁体   English

在 Angularjs 中完成页面加载后调用函数

[英]Call a function after complete page load in Angularjs

I am new to Angularjs and I am using following code to execute a function after page load.我是 Angularjs 的新手,我正在使用以下代码在页面加载后执行一个函数。 I have written this script in HTML.我已经用 HTML 编写了这个脚本。

<script type="text/javascript">
        $(document).ready(function(){
                showProvision(device.data.provision.endpoint_brand,device.data.provision.endpoint_model);
        });
</script>

I have used below code as well but unfortunately getting nothing whatever I want.我也使用了下面的代码,但不幸的是我什么都没有得到。

<script type="text/javascript">
        setTimeout(function() {
                showProvision(device.data.provision.endpoint_brand,device.data.provision.endpoint_model);
            }, 5000);

</script>

The parameters of the function will come from controller after the page load.该函数的参数将在页面加载后来自控制器。 After getting the parameters, I want to execute this function.拿到参数后,我想执行这个函数。 If there is any other option or choice to do it then tell me.如果有任何其他选择或选择,请告诉我。

Just to have this answer here for posterity, whenever you want Angular to take advantage of something external, you have to call that function after AngularJS has bootstrapped itself in the current document.只是为了给后代提供这个答案,每当您希望 Angular 利用外部的东西时,您都必须在 AngularJS 在当前文档中引导自己之后调用该函数。

Then, you can call it inside a controller, or create a service with it so it is reusable inside your application.然后,您可以在控制器中调用它,或者用它创建一个服务,以便它可以在您的应用程序中重用。

IF, there is a resource that is supposed to load after the application, you can either load the script in the page or you can lazy load it.如果有应该在应用程序之后加载的资源,您可以在页面中加载脚本,也可以延迟加载它。

For an Angular modules, you would need to use something similar to this.对于 Angular 模块,您需要使用类似的东西。

https://github.com/ocombe/ocLazyLoad https://github.com/ocombe/ocLazyLoad

For an external library, it will be best to use $scriptJS as the actual loading library, it also does AMD for you, it is really good.对于外部库,最好使用 $scriptJS 作为实际加载库,它也为你做 AMD,真的很好。 https://github.com/ded/script.js https://github.com/ded/script.js

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

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