最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - window.onload in Angular - Stack Overflow

programmeradmin1浏览0评论

Is there any equivalent of window.onload event in Angular?

I want to fade out and remove my preloader but only after all resources like images are loaded. Since $viewConteneLoaded indicates only html insertion into view (i am using ngRotue) listening to it is not sufficient.

I would like to capture window.onload event, but don't know how to do it properly and what is the best-practice for doing it in Angular, so that digest would fire.

Is there any equivalent of window.onload event in Angular?

I want to fade out and remove my preloader but only after all resources like images are loaded. Since $viewConteneLoaded indicates only html insertion into view (i am using ngRotue) listening to it is not sufficient.

I would like to capture window.onload event, but don't know how to do it properly and what is the best-practice for doing it in Angular, so that digest would fire.

Share Improve this question edited Feb 28, 2017 at 7:01 julew2 asked Feb 28, 2017 at 6:51 julew2julew2 2091 gold badge4 silver badges14 bronze badges 1
  • $window.onload = function() {}; try this. – Manikandan Velayutham Commented Feb 28, 2017 at 7:20
Add a ment  | 

5 Answers 5

Reset to default 2

Write the following in the ponent file:

import { HostListener } from '@angular/core';

...

@HostListener('window:load')
onLoad() {
  console.log('is window:load');
}

...

What about:

ngAfterViewInit(){
   // your code ...
}

It runs after the view is fully loaded.
https://angular.io/api/core/AfterViewInit#ngAfterViewInit

Try to use $window.onload event.

Example:

angular.element($window).bind('load', function() {
    //put your code
});

For Angular 12

angular.element(window).bind('load', function() {
    //put your code
});

Use angular's init() function to hide your preloader and run it at bottom of your page/controller so that all the content is finished loading before it reaches init function.

You can use angular.element(document).ready(); as below

 angular.element(document).ready(function () {
       //do something
    });
发布评论

评论列表(0)

  1. 暂无评论