WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

AngularJS Controllers


AngularJS controllers control the data of AngularJS applications.

AngularJS controllers are standard JavaScript Objects.


AngularJS Controllers

AngularJS applications are constrained by controllers.

The ng-controller order characterizes the application controller.

A controller is a JavaScript Object, made by a standard JavaScript object constructor.

AngularJS Example

<div ng-app="myApp" ng-controller="myCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Keep going Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

<script>
var application = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>
Try it Yourself »

Application explained:

The AngularJS application is characterized by  ng-app="myApp". The application keeps running inside the <div>.

The ng-controller="myCtrl" trait is an AngularJS order. It characterizes a controller.

The myCtrl work is a JavaScript function.

AngularJS will conjure the controller with a $scope object.

In AngularJS, $scope is the application object (the proprietor of use factors and functions).

The controller makes two properties (factors) in the degree (firstName and lastName).

The ng-model mandates tie the information fields to the controller properties (firstName and lastName).


Controller Methods

The precedent above showed a controller object with two properties: lastName and firstName.

A controller can likewise have strategies (factors as functions):

AngularJS Example

<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Keep going Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}

</div>

<script>
var application = angular.module('myApp', []);
app.controller('personCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
    $scope.fullName = work() {
        return $scope.firstName + " " + $scope.lastName;
    };
});
</script>
Try it Yourself »

Controllers In External Files

In bigger applications, usually to store controllers in outside files.

Just duplicate the code between the <script> labels into an outside document named personController.js:

AngularJS Example

<div ng-app="myApp" ng-controller="personCtrl">

First Name: <input type="text" ng-model="firstName"><br>
Keep going Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{firstName + " " + lastName}}

</div>

<script src="personController.js"></script>
Try it Yourself »

Another Example

For the following precedent we will make another controller file:

angular.module('myApp', []).controller('namesCtrl', function($scope) {
    $scope.names = [
        {name:'Jani',country:'Norway'},
        {name:'Hege',country:'Sweden'},
        {name:'Kai',country:'Denmark'}
    ];
});

Save the document as  namesController.js:

And then utilize the controller document in an application:

AngularJS Example

<div ng-app="myApp" ng-controller="namesCtrl">

<ul>
  <li ng-repeat="x in names">
    {{ x.name + ', ' + x.country }}
  </li>
</ul>

</div>

<script src="namesController.js"></script>
Try it Yourself »