In AngularJS, the controller is a Javascript function (type class) used to enhance the scope instance except for the root scope unexpected. When you or AngularJS itself through & lt; code & gt; scope. $ new & lt; code & gt; when you create a new sub-scope object, there is an option for you to pass it as a parameter to the controller. 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.
First Name:
Last Name:
Full Name: {{firstName + " " + lastName}}
《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 ng-app's
.
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 ):
First Name:
Last Name:
Full Name: {{fullName()}}
《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:
First Name:
Last Name:
Full Name: {firstName + "" + lastName }}