AngularJS controller
AngularJS controller controls data from AngularJS applications.
AngularJS controller is a regular JavaScript object.
AngularJS controller
AngularJS application is controlled by the controller.
The ng-controller directive defines the application controller.
A controller is a JavaScript object created by the constructor of a standard JavaScript object.
AngularJS instance
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="personCtrl">Name: <input type="text" ng-model="firstName"><br>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></body></html>Running results:
name:
surname:
Name: John Doe
AngularJS applications are defined by ng-app. The application runs within <div>.
The ng-controller="myCtrl" property 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 (belongs to application variables and functions).
The controller's $scope (equivalent to scope and control scope) is used to save the AngularJS Model object.
The controller creates two properties (firstName and lastName) in the scope.
The ng-model directive binds the input domain to the controller's properties (firstName and lastName).
Controller method
The above example demonstrates a controller object with two properties: lastName and firstName.
The controller can also have methods (variables and functions):
AngularJS instance
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="personCtrl">Name: <input type="text" ng-model="firstName"><br>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></body></html>Running effect:
name:
surname:
Name: John Doe
Controllers in external files
In large applications, the controller is usually stored in an external file.
Just copy the code in the <script> tag to an external file named personController.js:
AngularJS instance
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><div ng-app="myApp" ng-controller="personCtrl">Name: <input type="text" ng-model="firstName"><br>Name: <input type="text" ng-model="lastName"><br><br>Name: {{firstName + " " + lastName}}</div><script src="personController.js"></script></body></html>Running results:
name:
surname:
Name: John Doe
Other examples
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 the application:
AngularJS instance
<!DOCTYPE html><html><head><meta charset="utf-8"><script src="http://apps.bdimg.com/libs/angular.js/1.4.6/angular.min.js"></script></head><body><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></body></html>Running effect:
The above is the compilation of AngularJS controller data, and will be supplemented later.