This article mainly introduces the use of AngularJS controllers, and provides specific object examples for HTML, for more information, see AngularJS applications that rely mainly on controllers to control the flow of data in applications. The controller is defined using the ng-controller command. The controller is a function that contains attributes/attributes and JavaScript objects. Each controller accepts the $ scope parameter to specify the application/module, which is controlled by the controller.
...
Here, we have declared the controller studentController using the ng-controller command. As the next step, we will define studentController as follows:
《script》function studentController($scope) { $scope.student = { firstName: "yiibai", lastName: "com", fullName: function() { var studentObject; studentObject = $scope.student; return studentObject.firstName + " " + studentObject.lastName; } };}《script》
StudentController defines $ scope as a JavaScript object parameter.
$ Scope indicates the application and uses the studentController object.
$ Scope. student is the attribute of the studentController object.
FirstName and lastName are two attributes of the $ scope. student object. We have passed the default value to them.
FullName is the function of the $ scope. student object. Its task is to return the name of the merged object.
In the fullName function, we want the student object to return the name of the combination.
As a description, you can also define the controller object in a separate JS file and put the HTML page in the file.
You can use ng-model or the following expression to use studentController attributes.
Enter first name:
Enter last name:
You are entering: {{student.fullName()}}
There are two input boxes: student. firstName and student. lastname.
Now the student. fullName () method is added to HTML.
Now, you only need to enter what is in the first name and lastname input boxes, you can see that the two names are automatically updated.
Example
The following example shows how to use a controller.
The content of the testAngularJS.html file is as follows:
Angular JS ControllerAngularJS Sample ApplicationEnter first name:
Enter last name:
You are entering: {student. fullName ()}}
Script function studentController ($ scope) {$ scope. student = {firstName: "Mahesh", lastName: "Parashar", fullName: function () {var studentObject; studentObject = $ scope. student; return studentObject. firstName + "" + studentObject. lastName ;};} script