AngularJS basic learning notes controller, angularjs learning notes
AngularJS controller is used to control AngularJS applications.
AngularJS controller is a common JavaScript Object.
AngularJS Controller
AngularJS applications are controlled by controllers.
The ng-controller command defines an application controller.
A controller is a JavaScript Object, which can be created through a standard JavaScript object constructor.
<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>
Code explanation:
AngularJS application is defined by ng-app = "myApp. The valid scope of this application is in <div> where ng-app is located.
Ng-controller = "myCtrl" attribute is an AngularJS command, which defines a controller.
The myCtrl function is a common JavaScript function.
AngularJS uses the $ scope object to call the controller.
In AngularJS, $ scope is an application object (the owner of application variables and functions ).
The controller contains two attributes (or variables): firstName and lastName. They are appended to the $ scope object.
The ng-model command binds the value of the input tag to the attributes of the Controller (firstName and lastName ).
Controller Method
The preceding example shows that the controller object contains two attributes: lastName and firstName.
The controller can also contain methods (assign a function to a variable ):
<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>
Place the Controller in an external file
In large applications, controller code is often written in external files.
Copy the code in the <script> label to the external file of personController. js:
<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: {{firstName + " " + lastName}}</div><script src="personController.js"></script>
Another example
Create a new controller file named namesController. js:
angular.module('myApp', []).controller('namesCtrl', function($scope) { $scope.names = [ {name:'Jani',country:'Norway'}, {name:'Hege',country:'Sweden'}, {name:'Kai',country:'Denmark'} ];});
Then use the Controller file in the application:
<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>
The above is all the content of this article. I hope you will like it.