Background
[wp_ad_camp_5]
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
[wp_ad_camp_4]
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.
1 |
<div class="panel-body" data-ng-controller="MyController" data-ng-init="init()"> |
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.
1 2 3 4 5 6 7 8 |
angular.module('app.ws.a').controller('MyController', ['$scope', '$sanitize', '$filter', 'logger', function ($scope, $sanitize, $filter, logger) { $scope.init = function() { // initialize stuff here }; }]); |
[wp_ad_camp_3]