Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use window.onload in angular js controller?

Tags:

angularjs

How to use window.onload function inside a controller.

 window.onload= function() { 
    console.log("Hi Page loaded")
   };

The controller is not applicable for the full page. I want to execute a function after my controller loads at-least.

like image 766
Santosh Hegde Avatar asked Mar 11 '23 12:03

Santosh Hegde


1 Answers

You can use ng-init and invoke your necessary function using the same directive

var app = angular.module('DemoApp', [])
app.controller('akuaController', function($scope) {
  $scope.load = function() {
    alert("Window is loaded");
  }
});
<!DOCTYPE html>
<html>

<head>
  <script data-require="[email protected]" data-semver="1.4.7" src="https://code.angularjs.org/1.4.7/angular.js"></script>
  <script src="//cdnjs.cloudflare.com/ajax/libs/angular-filter/0.4.7/angular-filter.js"></script>
  <link rel="stylesheet" href="style.css" />
  <script src="script.js"></script>
</head>

<body ng-app="DemoApp" ng-controller="akuaController">
  <div ng-init="load()">
  </div>
</body>

</html>
like image 180
Sajeetharan Avatar answered Apr 02 '23 11:04

Sajeetharan