AngularJS - มุมมอง

AngularJS รองรับ Single Page Application ผ่านหลายมุมมองในหน้าเดียว ในการดำเนินการนี้ AngularJS ได้จัดเตรียมคำสั่ง ng-view และ ng-template และบริการ $ routeProvider

คำสั่ง ng-view

คำสั่ง ng-view จะสร้างตัวยึดตำแหน่งที่สามารถวางมุมมองที่เกี่ยวข้อง (มุมมอง HTML หรือ ng-template) ตามการกำหนดค่า

การใช้งาน

กำหนด div ด้วย ng-view ภายในโมดูลหลัก

<div ng-app = "mainApp">
   ...
   <div ng-view></div>

</div>

คำสั่ง ng-template

คำสั่ง ng-template ใช้เพื่อสร้างมุมมอง HTML โดยใช้แท็กสคริปต์ มันมีแอตทริบิวต์idซึ่ง $ routeProvider ใช้เพื่อแมปมุมมองกับคอนโทรลเลอร์

การใช้งาน

กำหนดบล็อกสคริปต์ด้วย type เป็น ng-template ภายในโมดูลหลัก

<div ng-app = "mainApp">
   ...
	
   <script type = "text/ng-template" id = "addStudent.htm">
      <h2> Add Student </h2>
      {{message}}
   </script>

</div>

$ routeProvider Service

$ routeProvider เป็นบริการหลักที่ตั้งค่าการกำหนดค่าของ URL แมปกับเพจ HTML หรือ ng-template ที่เกี่ยวข้องและแนบคอนโทรลเลอร์ด้วยสิ่งเดียวกัน

การใช้งาน 1

กำหนดบล็อกสคริปต์ด้วย type เป็น ng-template ภายในโมดูลหลัก

<div ng-app = "mainApp"> 
   ... 
   <script type = "text/ng-template" id = "addStudent.htm"> 
      <h2> Add Student </h2> 
      {{message}} 
   </script>  
</div>

การใช้งาน 2

กำหนดบล็อกสคริปต์ด้วยโมดูลหลักและตั้งค่าการกำหนดเส้นทาง

var mainApp = angular.module("mainApp", ['ngRoute']);

mainApp.config(['$routeProvider', function($routeProvider) {
   $routeProvider
   
   .when('/addStudent', {
      templateUrl: 'addStudent.htm', controller: 'AddStudentController'
   })
   .when('/viewStudents', {
      templateUrl: 'viewStudents.htm', controller: 'ViewStudentsController'
   })
   .otherwise ({
      redirectTo: '/addStudent'
   });
	
}]);

ประเด็นต่อไปนี้เป็นสิ่งสำคัญที่ต้องพิจารณาในตัวอย่างข้างต้น -

  • $ routeProvider ถูกกำหนดให้เป็นฟังก์ชันภายใต้ config ของโมดูล mainApp โดยใช้คีย์เป็น '$ routeProvider'

  • $ routeProvider. เมื่อกำหนด URL "/ addStudent" ซึ่งแมปกับ "addStudent.htm" addStudent.htm ควรอยู่ในเส้นทางเดียวกับหน้า HTML หลัก หากไม่ได้กำหนดหน้า HTML จะต้องใช้ ng-template กับ id = "addStudent.htm" เราใช้ ng-template

  • "มิฉะนั้น" ใช้เพื่อตั้งค่ามุมมองเริ่มต้น

  • "ตัวควบคุม" ใช้เพื่อตั้งค่าตัวควบคุมที่เกี่ยวข้องสำหรับมุมมอง

ตัวอย่าง

ตัวอย่างต่อไปนี้แสดงการใช้คำสั่งดังกล่าวข้างต้นทั้งหมด

testAngularJS.htm

<html>
   <head>
      <title>Angular JS Views</title>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script>
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular-route.min.js">
      </script>
   </head>
   
   <body>
      <h2>AngularJS Sample Application</h2>
      <div ng-app = "mainApp">
         <p><a href = "#addStudent">Add Student</a></p>
         <p><a href = "#viewStudents">View Students</a></p>
         <div ng-view></div>
         
         <script type = "text/ng-template" id = "addStudent.htm">
            <h2> Add Student </h2>
            {{message}}
         </script>
         
         <script type = "text/ng-template" id = "viewStudents.htm">
            <h2> View Students </h2>
            {{message}}
         </script>
      </div>
      
      <script>
         var mainApp = angular.module("mainApp", ['ngRoute']);
         mainApp.config(['$routeProvider', function($routeProvider) {
            $routeProvider
            
            .when('/addStudent', {
               templateUrl: 'addStudent.htm',
               controller: 'AddStudentController'
            })
            .when('/viewStudents', {
               templateUrl: 'viewStudents.htm',
               controller: 'ViewStudentsController'
            })
            .otherwise({
               redirectTo: '/addStudent'
            });
         }]);
         mainApp.controller('AddStudentController', function($scope) {
            $scope.message = "This page will be used to display add student form";
         });
         mainApp.controller('ViewStudentsController', function($scope) {
            $scope.message = "This page will be used to display all the students";
         });
      </script>
      
   </body>
</html>

เอาต์พุต

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