ANGULARJS controller detailed and sample code _ANGULARJS

Source: Internet
Author: User


Angularjs applications rely primarily on controllers to control the flow of data within the application. The controller is defined by the Ng-controller directive. A controller is a feature that contains properties/attributes and JavaScript objects. Each controller accepts the $scope parameter to specify the application/module, controlled by the controller.


<div ng-app= "" ng-controller= "Studentcontroller" > ...
</div>


Here, we have declared the controller studentcontroller using the ng-controller instruction. As a 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.



The $scope represents the application, using the Studentcontroller object.



$scope. Student is the property of the Studentcontroller object.



FirstName and LastName are two properties of the $scope.student object. We have passed the default values to them.



FullName is a function of the $scope.student object, whose task is to return the merged name.



In the FullName function, we now want the student object to return the combination name.



As a description, you can also define the controller object in a separate JS file and put the relevant file in the HTML page.



You can now use the Studentcontroller student's properties using Ng-model or using an expression as follows.


Enter 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 ()}}


Now there are student.firstname and student.lastname two input boxes.



There are now student.fullname () methods added to HTML.



Now, as soon as you enter what is entered in the first name and LastName input box, you can see two names automatically updated.



Example



The following example shows the use of a controller.



The contents of the testangularjs.html file are as follows:

Output



Open textangularjs.html in your Web browser and see the following results:






The above is the ANGULARJS controller data collation, follow-up continue to collate relevant knowledge, thank you for your support to 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.