AngularJS - คอนโทรลเลอร์

แอปพลิเคชัน AngularJS ส่วนใหญ่อาศัยคอนโทรลเลอร์เพื่อควบคุมการไหลของข้อมูลในแอปพลิเคชัน ตัวควบคุมถูกกำหนดโดยใช้คำสั่งng-controller คอนโทรลเลอร์คืออ็อบเจ็กต์ JavaScript ที่มีแอ็ตทริบิวต์ / คุณสมบัติและฟังก์ชัน คอนโทรลเลอร์แต่ละตัวยอมรับ $ scope เป็นพารามิเตอร์ซึ่งหมายถึงแอปพลิเคชัน / โมดูลที่คอนโทรลเลอร์จำเป็นต้องจัดการ

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

ที่นี่เราประกาศตัวควบคุมชื่อstudentControllerโดยใช้คำสั่ง ng-controller เรากำหนดไว้ดังนี้ -

<script>
   function studentController($scope) {
      $scope.student = {
         firstName: "Mahesh",
         lastName: "Parashar",
         
         fullName: function() {
            var studentObject;
            studentObject = $scope.student;
            return studentObject.firstName + " " + studentObject.lastName;
         }
      };
   }
</script>
  • studentController ถูกกำหนดให้เป็นวัตถุ JavaScript โดยมี $ scope เป็นอาร์กิวเมนต์

  • ขอบเขต $ หมายถึงแอปพลิเคชันที่ใช้วัตถุ studentController

  • $ scope.student เป็นคุณสมบัติของอ็อบเจ็กต์ studentController

  • firstName และ lastName เป็นคุณสมบัติสองอย่างของวัตถุ $ scope.student เราส่งค่าเริ่มต้นไปให้

  • คุณสมบัติ fullName เป็นฟังก์ชันของวัตถุ $ scope.student ซึ่งส่งคืนชื่อที่รวมกัน

  • ในฟังก์ชัน fullName เราได้รับวัตถุนักเรียนจากนั้นส่งคืนชื่อที่รวมกัน

  • หมายเหตุเราสามารถกำหนดออบเจ็กต์คอนโทรลเลอร์ในไฟล์ JS แยกต่างหากและอ้างอิงไฟล์นั้นในหน้า HTML

ตอนนี้เราสามารถใช้คุณสมบัตินักเรียนของ studentController โดยใช้ ng-model หรือใช้นิพจน์ดังนี้ -

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()}}
  • เราผูก student.firstName และ student.lastname ไว้กับช่องป้อนข้อมูลสองช่อง

  • เราผูก student.fullName () กับ HTML

  • ตอนนี้ทุกครั้งที่คุณพิมพ์อะไรก็ได้ในช่องใส่ชื่อและนามสกุลคุณจะเห็นชื่อเต็มได้รับการอัปเดตโดยอัตโนมัติ

ตัวอย่าง

ตัวอย่างต่อไปนี้แสดงการใช้คอนโทรลเลอร์ -

testAngularJS.htm

<html>
   <head>
      <title>Angular JS Controller</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
      </script>
   </head>
   
   <body>
      <h2>AngularJS Sample Application</h2>
      
      <div ng-app = "mainApp" ng-controller = "studentController">
         Enter first name: <input type = "text" ng-model = "student.firstName"><br>
         <br>
         Enter last name: <input type = "text" ng-model = "student.lastName"><br>
         <br>
         You are entering: {{student.fullName()}}
      </div>
      
      <script>
         var mainApp = angular.module("mainApp", []);
         
         mainApp.controller('studentController', function($scope) {
            $scope.student = {
               firstName: "Mahesh",
               lastName: "Parashar",
               
               fullName: function() {
                  var studentObject;
                  studentObject = $scope.student;
                  return studentObject.firstName + " " + studentObject.lastName;
               }
            };
         });
      </script>
      
   </body>
</html>

เอาต์พุต

เปิดไฟล์testAngularJS.htmในเว็บเบราว์เซอร์และดูผลลัพธ์