AngularJS controller details and sample code, angularjs sample code
AngularJS applications mainly rely 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.
<div ng-app="" ng-controller="studentController">...</div>
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: <input type="text" ng-model="student.firstName"><br>Enter last name: <input type="text" ng-model="student.lastName"><br><br>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:
Output
Open textangularjs.html in the webbrowser and see the following results:
The above is the information of AngularJS controller. We will continue to sort out relevant knowledge in the future. Thank you for your support for this site.