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

Hey! I'm Anil Singh. I author this blog. I'm Active Blogger, Programmer. I love learning new technologies, programming, blogging and participating the forum discussions more...
My Blogs - http://www.code-sample.com and http://www.code-sample.xyz
My Books - Google Amazon and Flipkart Book Store!

You Might Also Like
Post a Comment
www.code-sample.com/. Powered by Blogger.
ASK Questions
SQL Server NodeJs TypeScript JavaScript Angular SQL Server My Book