0

Execute a function for initialization when a Controller loads in AngularJS

Background

This article demonstrates how to call a function for initialization purposes when a controller loads in AngularJS.

Software Environment

  • Windows 7 Professional SP1
  • Notepad or Notepad++
  • AngularJS 1.3.8

Your View

In your HTML page, use data-ng-init to specify which controller function to execute when, in this case, the <div> loads effectively loading the controller. There is an alternative way to “initialize” stuff in a controller, but this one is cleaner and more elegant approach.

Your Controller

In your app.js (or whatever filename you gave your main AngularJS app file), create an init function that is bound to $scope.