AngularJS controller details and sample code, angularjs sample code

Source: Internet
Author: User

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.