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; };
}]);

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>
Try Now

See Also

AngularJS Built-in Filters
AngularJS Built-in Directives
AngularJS Expression
AngularJS Built-in Events
AngularJS Scope Methods
AngularJS Built-in Services
AngularJS Controller Inheritance
AngularJS Custom Directives


 
 
SHARE THIS IF YOU LIKE!
submit to reddit

SUBSCRIBE FOR LATEST UPDATES
HAVE WE CONNECTED ON FACEBOOK?
TOP TUTORIALSTOP CODE EXAMPLESTOP LIVE DEMOLINKSSTAY CONNECTED
HTML Tutorial
HTML5 Tutorial
Bootstrap3 Tutorial
Javascript Tutorial
TypeScript Tutorial
AngularJS Tutorial
CSharp Tutorial
PHP Tutorial
Developer Tools
Javascript
AngularJS
Google MAP API V3
ASP.NET
Javascript
AngularJS
Google MAP API V3
Contact Us
Advertise with Us
Privacy Policy
Disclaimer
Stay connected. We actually like you. If you want to get up to the minute updates, then connect with us on your fav below.
 
Twitter   Linkedin   Facebook   Google+    RSS   Pinterest
 
Protected by Copyscape Original Content Checker
 
 
Copyright ©2017 www.techstrikers.com Unauthorized reproduction/replication of any part of this site is prohibited.