---
title: "How to use Controller in AngularJS"  
description: "How to use Controller in AngularJS"  
author: "Anonymous User"  
published: 2020-11-26  
updated: 2020-11-26  
canonical: https://www.mindstick.com/forum/156118/how-to-use-controller-in-angularjs  
category: "angular js"  
tags: ["angular js"]  
reading_time: 2 minutes  

---

# How to use Controller in AngularJS

i am started working on [angular](https://www.mindstick.com/articles/13081/advantages-disadvantages-of-angularjs-is-that-ideal-for-your-project) js no i [am trying](https://answers.mindstick.com/qa/36834/which-two-programming-languages-should-i-master-in-if-i-am-trying-to-get-into-google-or-facebook) to use [Controller](https://www.mindstick.com/blog/273/passing-values-from-controller-to-view-in-asp-dot-net-mvc) in [AngularJS](https://www.mindstick.com/forum/33926/use-angularjs-without-scope-in-mvc) how to do it Please help me.

## Replies

### Reply by Anonymous User

Hi ,

Here we will explain how to use controller in AngularJS.

AngularJS controller to control the flow of data in the application. AngularJS controller is defined with ng-controller directive. AngularJS controller is a JavaScript object containing attributes/properties and functions. AngularJS controller accepts $scope as a parameter which refers to the application/module that controller is to control.

Now I am going to declared a controller MyController using ng-controller directive. See Below –

## MyController

```
<script>
    var mainApp = angular.module("MyApp", []);
     mainApp.controller('MyController', function ($scope) {
         $scope.User = {
             Name: "Aditya Kumar Patel",
             Age: "24"  };
    });
 </script>
```

Above Code MyController defined as a JavaScript object with $scope as argument. $scope refers to application which is to use the MyController object. $scope.User is property of MyController object. Name and Age are two properties of $scope.User object. We've defined the default values Name=”Aditya Kumar Patel” and Age=”24”

Now I am going to use controller in our HTML page . show below-

## Simple Example

```
<script src="~/Scripts/jquery-3.1.1.js"></script><script src="~/Scripts/angular.js"></script><h2>AngularJS Application</h2><div ng-app="MyApp" ng-controller="MyController">    Enter Name: <input type="text" ng-model="User.Name"><br><br>    Enter Age: <input type="text" ng-model="User.Age"><br><br>
    Name : {{User.Name}}     Age : {{User.Age}}
</div>
<script>    var mainApp = angular.module("MyApp", []);     mainApp.controller('MyController', function ($scope) {         $scope.User = {             Name: "Aditya Kumar Patel",             Age: "24"  };
});
</script>
```

Now we run application and see Output

![How to use Controller in AngularJS](https://www.mindstick.com/mindstickforums/72de924f-64d7-4bd4-81be-e9aacfd27de4/images/44e20cdc-eabe-476d-a67d-991e6944a506.png)\

## \


---

Original Source: https://www.mindstick.com/forum/156118/how-to-use-controller-in-angularjs

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
