欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

AngularJS 依賴(lài)注入詳解和簡(jiǎn)單實(shí)例

 更新時(shí)間:2016年07月28日 15:25:03   投稿:lqh  
本文主要介紹AngularJS 依賴(lài)注入,這里對(duì)依賴(lài)注入做了詳細(xì)介紹講解,并提供效果圖和示例代碼以便學(xué)習(xí)參考

AngularJS 依賴(lài)注入

什么是依賴(lài)注入

wiki 上的解釋是:依賴(lài)注入(Dependency Injection,簡(jiǎn)稱(chēng)DI)是一種軟件設(shè)計(jì)模式,在這種模式下,一個(gè)或更多的依賴(lài)(或服務(wù))被注入(或者通過(guò)引用傳遞)到一個(gè)獨(dú)立的對(duì)象(或客戶(hù)端)中,然后成為了該客戶(hù)端狀態(tài)的一部分。
該模式分離了客戶(hù)端依賴(lài)本身行為的創(chuàng)建,這使得程序設(shè)計(jì)變得松耦合,并遵循了依賴(lài)反轉(zhuǎn)和單一職責(zé)原則。與服務(wù)定位器模式形成直接對(duì)比的是,它允許客戶(hù)端了解客戶(hù)端如何使用該系統(tǒng)找到依賴(lài)

一句話(huà) --- 沒(méi)事你不要來(lái)找我,有事我會(huì)去找你。

AngularJS 提供很好的依賴(lài)注入機(jī)制。以下5個(gè)核心組件用來(lái)作為依賴(lài)注入:

value
factory
service
provider
constant

value

Value 是一個(gè)簡(jiǎn)單的 javascript 對(duì)象,用于向控制器傳遞值(配置階段):

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

// 創(chuàng)建 value 對(duì)象 "defaultInput" 并傳遞數(shù)據(jù)
mainApp.value("defaultInput", 5);
...

// 將 "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

factory 是一個(gè)函數(shù)用于返回值。在 service 和 controller 需要時(shí)創(chuàng)建。
通常我們使用 factory 函數(shù)來(lái)計(jì)算或返回值。

// 定義一個(gè)模塊
var mainApp = angular.module("mainApp", []);

// 創(chuàng)建 factory "MathService" 用于兩數(shù)的乘積 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;
}); 

// 在 service 中注入 factory "MathService"
mainApp.service('CalcService', function(MathService){
  this.square = function(a) {
   return MathService.multiply(a,a);
  }
});
...

provider

AngularJS 中通過(guò) provider 創(chuàng)建一個(gè) service、factory等(配置階段)。

Provider 中提供了一個(gè) factory 方法 get(),它用于返回 value/service/factory。

// 定義一個(gè)模塊
var mainApp = angular.module("mainApp", []);
...

// 使用 provider 創(chuàng)建 service 定義一個(gè)方法用于計(jì)算兩數(shù)乘積
mainApp.config(function($provide) {
  $provide.provider('MathService', function() {
   this.$get = function() {
     var factory = {}; 
     
     factory.multiply = function(a, b) {
      return a * b; 
     }
     return factory;
   };
  });
});

constant

constant(常量)用來(lái)在配置階段傳遞數(shù)值,注意這個(gè)常量在配置階段是不可用的。

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

實(shí)例

以下實(shí)例提供了以上幾個(gè)依賴(lài)注入機(jī)制的演示。

<html>
  
  <head>
   <meta charset="utf-8">
   <title>AngularJS 依賴(lài)注入</title>
  </head>
  
  <body>
   <h2>AngularJS 簡(jiǎn)單應(yīng)用</h2>
   
   <div ng-app = "mainApp" ng-controller = "CalcController">
     <p>輸入一個(gè)數(shù)字: <input type = "number" ng-model = "number" /></p>
     <button ng-click = "square()">X<sup>2</sup></button>
     <p>結(jié)果: {{result}}</p>
   </div>
   
   <script src="http://apps.bdimg.com/libs/angular.js/1.4.6/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>

 運(yùn)行結(jié)果:

 

 以上就是對(duì)AngularJS 依賴(lài)注入資料整理,后續(xù)繼續(xù)補(bǔ)充,希望能幫助開(kāi)發(fā)AngularJS 的朋友。

相關(guān)文章

  • angularjs 獲取默認(rèn)選中的單選按鈕的value方法

    angularjs 獲取默認(rèn)選中的單選按鈕的value方法

    下面小編就為大家分享一篇angularjs 獲取默認(rèn)選中的單選按鈕的value方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2018-02-02
  • angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動(dòng)效果

    angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動(dòng)效果

    這篇文章主要為大家詳細(xì)介紹了angularjs創(chuàng)建彈出框?qū)崿F(xiàn)拖動(dòng)效果的相關(guān)資料,angularjs modal模態(tài)框創(chuàng)建可拖動(dòng)的指令,感興趣的小伙伴們可以參考一下
    2016-01-01
  • Angularjs過(guò)濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能示例

    Angularjs過(guò)濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能示例

    這篇文章主要介紹了Angularjs過(guò)濾器實(shí)現(xiàn)動(dòng)態(tài)搜索與排序功能,涉及AngularJS過(guò)濾器相關(guān)搜索、查詢(xún)、排序操作技巧,需要的朋友可以參考下
    2017-12-12
  • Angular指令封裝jQuery日期時(shí)間插件datetimepicker實(shí)現(xiàn)雙向綁定示例

    Angular指令封裝jQuery日期時(shí)間插件datetimepicker實(shí)現(xiàn)雙向綁定示例

    這篇文章主要介紹了Angular指令封裝jQuery日期時(shí)間插件datetimepicker實(shí)現(xiàn)雙向綁定示例,具有一定的參考價(jià)值,有興趣的可以了解一下。
    2017-01-01
  • AngularJS中的指令全面解析(必看)

    AngularJS中的指令全面解析(必看)

    下面小編就為大家?guī)?lái)一篇AngularJS中的指令全面解析(必看)。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2016-05-05
  • 最新評(píng)論