AngularJs call function on page load

AngularJs call function on page load

Hello everyone, I am going to share the code-sample for call function on page load in angular, The example for the same as given below.

The live plunker URL http://embed.plnkr.co/C2stGb/

HTML code-sample

<div ng-app="pageLoadApp" ng-controller="pageLoadAppCtrl">
  <div data-ng-init="onloadFun()">
     <a href="http://www.code-sample.com/" target="_blank">Welcome you!</a>
  </div>
</div>

AngularJs code-sample

var app = angular.module('pageLoadApp', []);

app.controller('pageLoadAppCtrl',  ["$scope", function($scope) {
    $scope.onloadFun = function() {
    alert(1);
    }
}]);

Full Running code as given below

<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>angularjs call function on page load</title>
    <script src="https://code.angularjs.org/1.4.9/angular.js"></script>
    <script>
    var app = angular.module('pageLoadApp', []);

    app.controller('pageLoadAppCtrl', ["$scope", function($scope) {
      $scope.onloadFun = function() {
        alert(1);
      }
    }]);
    </script>
</head>
<body ng-app="pageLoadApp" ng-controller="pageLoadAppCtrl">
    <div data-ng-init="onloadFun()">
        <a href="http://www.code-sample.com/" target="_blank">Welcome you! </a>
    </div>
</body>
</html>


I think, It might help you! Thank you!

ANIL SINGH

Anil Singh is an author, tech blogger, and software programmer. Book writing, tech blogging is something do extra and Anil love doing it. For more detail, kindly refer to this link..
My Blogs - https://code-sample.com and https://code-sample.xyz
My Book1 - BEST SELLING ANGULAR BOOK (INCLUDING ALL VERSIONS 2, 4, 5, 6, 7)
My Book2 - ANGULAR 2 INTERVIEW QUESTIONS BOOK - Both Books are Available on WorldWide.

www.code-sample.com/. Powered by Blogger.