AngularJS - การฉีดแบบพึ่งพา

Dependency Injection คือการออกแบบซอฟต์แวร์ที่ส่วนประกอบต่างๆจะได้รับการพึ่งพาแทนการเข้ารหัสอย่างหนักภายในส่วนประกอบ ช่วยลดส่วนประกอบจากการค้นหาการอ้างอิงและทำให้การอ้างอิงสามารถกำหนดค่าได้ นอกจากนี้ยังช่วยในการทำให้ส่วนประกอบสามารถนำกลับมาใช้ใหม่บำรุงรักษาและทดสอบได้

AngularJS มีกลไก Dependency Injection ที่ดีเยี่ยม มีส่วนประกอบหลักดังต่อไปนี้ซึ่งสามารถฉีดเข้าด้วยกันเป็นการอ้างอิง

  • Value
  • Factory
  • Service
  • Provider
  • Constant

มูลค่า

Value เป็นอ็อบเจ็กต์ JavaScript ธรรมดาซึ่งจำเป็นสำหรับการส่งผ่านค่าไปยังคอนโทรลเลอร์ในระหว่างขั้นตอนการกำหนดค่า (เฟสการกำหนดค่าคือเมื่อ AngularJS bootstraps เอง)

//define a module
var mainApp = angular.module("mainApp", []);

//create a value object as "defaultInput" and pass it a data.
mainApp.value("defaultInput", 5);
...

//inject the value in the controller using its name "defaultInput"
mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {
   $scope.number = defaultInput;
   $scope.result = CalcService.square($scope.number);
   
   $scope.square = function() {
      $scope.result = CalcService.square($scope.number);
   }
});

โรงงาน

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

//define a module
var mainApp = angular.module("mainApp", []);

//create a factory "MathService" which provides a method multiply to return multiplication of two numbers
mainApp.factory('MathService', function() {
   var factory = {};
   
   factory.multiply = function(a, b) {
      return a * b
   }
   return factory;
}); 

//inject the factory "MathService" in a service to utilize the multiply method of factory.
mainApp.service('CalcService', function(MathService) {
   this.square = function(a) {
      return MathService.multiply(a,a);
   }
});
...

บริการ

Service เป็นอ็อบเจ็กต์ JavaScript แบบซิงเกิลตันที่มีชุดของฟังก์ชันสำหรับทำงานบางอย่าง เซอร์วิสถูกกำหนดโดยใช้ฟังก์ชัน service () จากนั้นจะถูกฉีดเข้าไปในคอนโทรลเลอร์

//define a module
var mainApp = angular.module("mainApp", []);
...

//create a service which defines a method square to return square of a number.
mainApp.service('CalcService', function(MathService) {
   this.square = function(a) {
      return MathService.multiply(a,a); 
   }
});

//inject the service "CalcService" into the controller
mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {
   $scope.number = defaultInput;
   $scope.result = CalcService.square($scope.number);
   
   $scope.square = function() {
      $scope.result = CalcService.square($scope.number);
   }
});

ผู้ให้บริการ

AngularJS ใช้ผู้ให้บริการภายในเพื่อสร้างบริการโรงงาน ฯลฯ ในระหว่างขั้นตอนการกำหนดค่า สามารถใช้สคริปต์ต่อไปนี้เพื่อสร้าง MathService ที่เราสร้างไว้ก่อนหน้านี้ ผู้ให้บริการเป็นวิธีการพิเศษจากโรงงานที่มีเมธอด get () ซึ่งใช้ในการคืนค่า / บริการ / โรงงาน

//define a module
var mainApp = angular.module("mainApp", []);
...

//create a service using provider which defines a method square to return square of a number.
mainApp.config(function($provide) {
   $provide.provider('MathService', function() {
      this.$get = function() {
         var factory = {};  
         
         factory.multiply = function(a, b) {
            return a * b; 
         }
         return factory;
      };
   });
});

คงที่

ค่าคงที่ถูกใช้เพื่อส่งผ่านค่าในเฟสการกำหนดค่าโดยพิจารณาจากข้อเท็จจริงที่ว่าไม่สามารถใช้ค่าในระหว่างเฟสการกำหนดค่าได้

mainApp.constant("configParam", "constant value");

ตัวอย่าง

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

testAngularJS.htm

<html>
   <head>
      <title>AngularJS Dependency Injection</title>
   </head>
   
   <body>
      <h2>AngularJS Sample Application</h2>
      
      <div ng-app = "mainApp" ng-controller = "CalcController">
         <p>Enter a number: <input type = "number" ng-model = "number" /></p>
         <button ng-click = "square()">X<sup>2</sup></button>
         <p>Result: {{result}}</p>
      </div>
      
      <script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js">
      </script>
      
      <script>
         var mainApp = angular.module("mainApp", []);
         
         mainApp.config(function($provide) {
            $provide.provider('MathService', function() {
               this.$get = function() {
                  var factory = {};
                  
                  factory.multiply = function(a, b) {
                     return a * b;
                  }
                  return factory;
               };
            });
         });
			
         mainApp.value("defaultInput", 5);
         
         mainApp.factory('MathService', function() {
            var factory = {};
            
            factory.multiply = function(a, b) {
               return a * b;
            }
            return factory;
         });
         mainApp.service('CalcService', function(MathService) {
            this.square = function(a) {
               return MathService.multiply(a,a);
            }
         });
         mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {
            $scope.number = defaultInput;
            $scope.result = CalcService.square($scope.number);

            $scope.square = function() {
               $scope.result = CalcService.square($scope.number);
            }
         });
      </script>
      
   </body>
</html>

เอาต์พุต

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