Angularjs Examples

AJS Examples


AngularJS Controller Function

In AngularJS, if you want to do some manipulation in the view you must provide or add behavior in the scope object. AngularJS allow you to add behavior in the form of function or method in $scope object. You can than use these method in your view by calling the method from controller.

Syntax

myApp.controller('myController', ['$scope', function($scope) {
  $scope.sqrt = function(value) { return value * value; };
}]);

AngularJS Controller Function Example

<html ng-app="yourApp">
<head>
<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('yourApp', []);
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>
See Live Example