AngularJS: Controller Functions

To create a controller function that can be use with an element’s event attribute, you need to define a function with it’s implementation in it and then attached it to the $scope.

Below I have created a new Javascript file called app.js with a controller called AppController. In the $scope I have a property for storing text and one for attaching a function called onClick. When the onClick function is invoked, the message property will be set with a message ‘Hello, World!’:

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

// Controllers
app.controller('AppController', ['$scope', function($scope) {
		$scope.message = '';
		$scope.onClick = function() {
			$scope.message = 'Hello, World!';

Below I create a html page with body that uses the AppController controller, I have a input element that is a button type and I use ng-click to bind the function that I have create in the controller. I have created a div that will display the message property every time the button is clicked:

<html ng-app="app.demo">
	<title>Learn AngularJS - Functions</title>
	<script src=""></script>
	<script src="app.js"></script>
	<body ng-controller="AppController">
		<input type="button" ng-click="onClick()" value="Click Here" />

Download Source Code

AngularJS: Controllers

What is a Controller

If you are familiar with the MVC architectural pattern then controllers are nothing new to you. But to for those that are not, the controller’s job is to receive input from the view and updates the model or get data from the model and and update the view. For further information about Model View Controllers click here.

By using the ng-controller directive on your html page (view) allows you to associate AngularJS controller to a view.

<div ng-controller="HelloWorldController">


Python: Functions

Creating a function in Python allows you to reuse code throughout your project. You can create a library of useful functions that can make your coding simplified.

To create a function use the def statement follow by the name of the function and then an open parenthesis and then follow by a list of parameters and then a closing parenthesis and a colon. Then the next line you press TAB to indent your function’s code.

Your function can return a value by specifying the return statement follow by some value.

Create a new file called with the following code, or enter the code in your interpreter:

def add(a, b):
   return a + b

result = add(2,5)
print result

In the code above I have defined my function called Add with two parameters a and b which will take in an integer value. Then in the function body I am adding a and b together and return the result.

The next line I call the function Add and give it a value of 2 and 5 to be added and then assign the returned value to a variable called result, then I use the print statement to display the result. The expected value return should be displayed is 7.

Download the source code for this blog from my GitHub repository: