Angularjs Examples

AJS Examples


How to Create Controller Function in AngularJS

In this AngularJS basic example you will learn how to create controller function in AngularJS.

Here you can view the output of the example and you can also "try it yourself" by clicking on "Live Demo" button given at the bottom.

Syntax

myApp.controller('myController', ['$scope', function($scope) {  
  $scope.sqrt = function(value) { return value * value; };  
}]);
What is Expression in AngularJS
<html ng-app="myapp">  
<head> <!-- www.techstrikers.com -->
<title>My first AngularJS Controllers Function code</title>  
<script SRC="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.13/angular.js">  
</script>  
<script type="text/javascript">  
//Creating Modules and Controller here  
var app = angular.module('myapp', []);  
app.controller('myController', function($scope) {  
  $scope.sqrt = function(value) { return value * value; };  
})
</script>  
</head>  
<body>  
<div ng-controller="myController">  
  The Square of 20 equals to : {{ sqrt(20) }}  
</div>  
</body>  
</html>