WEB DEVELOPER SITE
HTMLCSSJAVASCRIPTSQLPHPBOOTSTRAPJQUERYANGULARXML
 

AngularJS Controllers


 AngularJS Controller Control Data for AngularJS applications.

 AngularJS Controllers are regular JavaScript objects .


AngularJS controller

AngularJS applications are controlled by controllers.

The

ng-controller directive defines an application controller.

The controller is a JavaScript object , created 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 resolution:

AngularJS applications are defined by ng-app . Application in <div> Within.

The

ng-controller = "myCtrl" attribute is an AngularJS directive. Used to define a controller.

The

myCtrl function is a JavaScript function.

AngularJS uses the $ scope object to call the controller.

In AngularJS, $ scope is an application object (belonging to application variables and functions).

The controller's $ scope (equivalent to scope, control scope) is used to save the AngularJS Model object.

The controller created two attributes in scope ( firstName and lastName ).

The

ng-model directive binds the input domain to the attributes of the controller (firstName and lastName).

Controller Methods

The example above demonstrates a controller object with two properties, lastName and firstName.

The controller can also have methods (variables and 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 large applications, the controller is usually stored in an external file.

Just put <script> Copy the code in the tag into an external file 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

The following example creates a new 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 file as namesController.js: Then, use the controller file in your 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 »