Angular Controllers

AngularJS Controllers


AngularJS controllers control the data of AngularJS applications.
AngularJS controllers are regular JavaScript Objects.

AngularJS Controllers

AngularJS applications are controlled past times controllers.
The ng-controller directive defines the application controller.
H5N1 controller is a JavaScript Object, created past times a measure JavaScript object constructor.

AngularJS Example

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

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

</div>

<script>
var app = angular.module('myApp', []);
app.controller('myCtrl', function($scope) {
    $scope.firstName = "John";
    $scope.lastName = "Doe";
});
</script>
Application explained:
The AngularJS application is defined by  ng-app="myApp". The application runs within the <div>.
The ng-controller="myCtrl" attribute is an AngularJS directive. It defines a controller.
The myCtrl component subdivision is a JavaScript function.
AngularJS volition invoke the controller alongside a $scope object.
In AngularJS, $scope is the application object (the possessor of application variables too functions).
The controller creates ii properties (variables) inwards the range (firstName too lastName).
The ng-model directives bind the input fields to the controller properties (firstName too lastName).

Controller Methods

The instance inwards a higher house demonstrated a controller object alongside ii properties: lastName too firstName.
H5N1 controller tin likewise convey methods (variables equally functions):

AngularJS Example

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

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

</div>

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

Controllers In External Files

In larger applications, it is mutual to shop controllers inwards external files.
Just re-create the code betwixt the <script> tags 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>
Last Name: <input type="text" ng-model="lastName"><br>
<br>
Full Name: {{fullName()}}

</div>

<script src="personController.js"></script>

Another Example

For the side past times side instance nosotros volition practise a novel 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:
And too thence work the controller file inwards an application:

AngularJS Example

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

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

</div>

<script src="namesController.js"></script>


Comments

Popular posts from this blog

What Are The Main Components of a Computer System

Top Qualities To Look For In An IT Support Team

How To Integrate Google Adwords Api Into Codeigniter?