window.onload does not work in AngularJS

二次信任 提交于 2019-11-29 03:24:38

Call your function with ng-init

var app = angular.module('app',[]);
app.controller('myController', function($scope){
    $scope.load = function () {
        alert("load event detected!");
    }
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app='app'>
  <div ng-controller='myController' ng-init='load()'></div>
</div>
Αλέκος

I prefer putting this kind of code in the app.run() function of angular.

e.g.

angular
.module('testApp', ['someModule'])
.constant('aConstant', 'hi')
.config(function($rootProvider) {/*some routing here*/})
.run(['$window', function($window) {
  $window.onload = function() {/*do your thing*/};
}]);

also check this nice post that depicts the order that some things happen in angular AngularJS app.run() documentation?

Heinrich

the following should work:

jQuery(function(){ /** my window onload functions **/ })

since angular uses a subset of jquery anyways you also may include the real thing.

better yet:
Instead of using this, you may consider using the angular way of initialising things:

that would be: http://docs.angularjs.org/api/ng.directive:ngInit

< any ng-init="functionInController(something)"...

to make it invisible until init: http://docs.angularjs.org/api/ng.directive:ngCloak

< any ng-cloak .....

to initialise/customize whole parts: http://docs.angularjs.org/guide/directive

< any directive-name....

Try

angular.element($window).bind('load', function() {
});
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!