zoukankan      html  css  js  c++  java
  • AngularJS依赖注入

    依赖注入是一个在组件中给出的替代了硬的组件内的编码它们的依赖关系的软件设计模式。这减轻一个组成部分,从定位的依赖,依赖配置。这有助于使组件可重用,维护和测试。

    AngularJS提供了一个至高无上的依赖注入机制。它提供了一个可注入彼此依赖下列核心组件。

    • 工厂

    • 服务

    • 提供者

    • 常值

    值是简单的JavaScript对象,它是用来将值传递过程中的配置相位控制器。

    //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);
       }
    });
    

    工厂

    工厂是用于返回函数的值。它根据需求创造值,每当一个服务或控制器需要。它通常使用一个工厂函数来计算并返回对应值

    //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); 
       }
    });
    ...
    

    服务

    服务是一个单一的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内部创建过程中配置阶段的服务,工厂等(相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.html

     1 <html>
     2 <head>
     3     <title>AngularJS Dependency Injection</title>
     4 </head>
     5 <body>
     6 <h2>AngularJS Sample Application</h2>
     7 <div ng-app="mainApp" ng-controller="CalcController">
     8     <p>Enter a number: <input type="number" ng-model="number" />
     9         <button ng-click="square()">X<sup>2</sup></button>
    10     <p>Input: {{number}}</p>
    11     <p>Result: {{result}}</p>
    12 </div>
    13 <script src="../../js/angular.js"></script>
    14 <script>
    15     var mainApp = angular.module("mainApp", []);
    16 
    17     mainApp.config(function($provide) {
    18         $provide.provider('MathService', function() {
    19             this.$get = function() {
    20                 var factory = {};
    21                 factory.multiply = function(a, b) {
    22                     return a * b;
    23                 }
    24                 return factory;
    25             };
    26         });
    27     });
    28 
    29     mainApp.value("defaultInput", 5);
    30 
    31     mainApp.factory('MathService', function() {
    32         var factory = {};
    33         factory.multiply = function(a, b) {
    34             return a * b;
    35         }
    36         return factory;
    37     });
    38 
    39     mainApp.service('CalcService', function(MathService){
    40         this.square = function(a) {
    41             return MathService.multiply(a,a);
    42         }
    43     });
    44 
    45     mainApp.controller('CalcController', function($scope, CalcService, defaultInput) {
    46         $scope.number = defaultInput;
    47         $scope.result = CalcService.square($scope.number);
    48 
    49         $scope.square = function() {
    50             $scope.result = CalcService.square($scope.number);
    51         }
    52     });
    53 </script>
    54 </body>
    55 </html>

    结果

    在Web浏览器打开textAngularJS.html。看到结果如下。

    AngularJS Dependency Injection

  • 相关阅读:
    PhotoshopCS6中文版图像处理实战从入门到精通
    Web安全开发指南
    OpenStack运维指南
    Word/Excel/PPT 2016高效办公实战从入门到精通
    UG NX 8.5中文版基础教程
    Moldflow 2018模流分析从入门到精通:升级版
    数据库与数据处理:Access 2010实现
    iOS开发网络数据之AFNetworking使用1
    AFNetworking2.5使用2
    iOS项目的完整重命名方法图文教程
  • 原文地址:https://www.cnblogs.com/laneyfu/p/4552280.html
Copyright © 2011-2022 走看看