简体   繁体   English

在AngularJS中加载页面后触发jQuery函数

[英]Trigger jQuery function after page has loaded in AngularJS

I just started learning AngularJS and I'm not really familiar with the terminologies in AngularJS yet. 我刚刚开始学习AngularJS,但是我对AngularJS的术语还不是很熟悉。 Here's my concern, I created a Loading Animation that shows up when I go to pages, but then I want to stop the Loading Animation as soon as all the contents of the pages has loaded. 我担心的是,我创建了一个加载动画,当我转到页面时会显示该动画,但是我想在页面的所有内容加载完毕后立即停止加载动画。 It's fairly easy to do in plain jQuery since I can just trigger $(window).load() but it doesn't seem to work in angular. 在普通的jQuery中这很容易做到,因为我可以触发$(window).load()但它似乎不能在角度工作。 $(document).ready() seems to work but that's not what I actually need since it gets triggered even though the images are not finished loading yet. $(document).ready()似乎可以工作,但这并不是我真正需要的,因为即使图像尚未完成加载,它也会被触发。 I already tried $scope.init inside my controller as well as $window.onload inside my controller but I still can't make it work. 我已经在控制器内部尝试了$scope.init以及在控制器内部尝试$scope.init $window.onload但仍然无法使其正常工作。

Hotfix answer 修补程序答案

Actually you do not need to wait for any of these events in angular. 实际上,您不需要等待所有这些事件。 Just use $().. in your angular controller - the site was already loaded. 只需在您的角度控制器中使用$()..-该站点已加载。 (similar to the window.load() event. In case jquery does not find the html elements please try to wrap it with $timeout( function() { ... } ); (类似于window.load()事件。如果jquery找不到html元素,请尝试用$ timeout(function(){...})包装它;

Recommendation 建议

Please do not! 请不要! There are angular ways to animate stuff which fits better than crazy $('#id'). 有一些角度化的方法可以使内容动画化,比疯狂的$('#id')更适合。 logic. 逻辑。 This will break in growing applications. 这将在不断增长的应用程序中打破。

I would recommend you forget about jQuery when you are working on AngularJS application. 我建议您在使用AngularJS应用程序时忘掉jQuery。 So figure out how to work everything out in AngularJS way - move all jQuery logic that you have to angular controller and avoid direct DOM manipulation (jQuery way). 因此,弄清楚如何以AngularJS的方式处理所有事情-将所有您需要的jQuery逻辑移至angular控制器,并避免直接进行DOM操作(jQuery方式)。

Also, there's a good tutorial online if you are moving to AngularJS from jQuery: 另外,如果您要从jQuery迁移到AngularJS,也可以在线获得一个很好的教程:

https://gabrieleromanato.name/introduction-to-angularjs-for-jquery-developers https://gabrieleromanato.name/introduction-to-angularjs-for-jquery-developers

If you need a solution for switching pages go with ui-router - it will give you even more flexibility with loading pages and resolving properties for different pages: 如果您需要切换页面的解决方案,请使用ui-router-在加载页面和解析不同页面的属性时,它将为您提供更大的灵活性:

https://github.com/angular-ui/ui-router https://github.com/angular-ui/ui-router

And in this particular case you can use simple boolean property on $scope and show preloader div based on that: 在这种情况下,您可以在$ scope上使用简单的boolean属性,并根据该值显示preloader div:

<div ng-show="showOverlay" class="loader" />

then inside your controller you could put something like: 然后在控制器内部,您可以输入以下内容:

$scope.showOverlay = true;

and then after your page logic is loaded and promises are resolved you could just hide that preloader with: 然后在加载页面逻辑并兑现承诺后,您可以使用以下命令隐藏该预加载器:

$scope.showOverlay = false;

To illustrate I created simple fiddle with preloader for you. 为了说明,我为您创建了带有预加载器的简单小提琴。 Also keep in mind there are tons of different ways of implementing this but this is simple one that should work for almost any case: 还请记住,有很多实现此问题的不同方法,但这是一种简单的方法,几乎​​可以在任何情况下使用:

https://jsfiddle.net/pegla/ng1mn8qp/3/ https://jsfiddle.net/pegla/ng1mn8qp/3/

There's also one answer here on stack overflow that could help you: 堆栈溢出还有一个答案可以帮助您:

How to execute angular controller function on page load? 如何在页面加载时执行角度控制器功能?

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

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