最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

AngularJS 依賴注入詳解及示例代碼

 更新時(shí)間:2016年08月17日 15:26:46   作者:bbflys  
本文主要介紹AngularJS 依賴注入的知識(shí),這里整理了相關(guān)的基礎(chǔ)知識(shí),并附示例代碼和實(shí)現(xiàn)效果圖,有興趣的小伙伴可以參考下

依賴注入是一個(gè)在組件中給出的替代了硬的組件內(nèi)的編碼它們的依賴關(guān)系的軟件設(shè)計(jì)模式。這減輕一個(gè)組成部分,從定位的依賴,依賴配置。這有助于使組件可重用,維護(hù)和測(cè)試。

AngularJS提供了一個(gè)至高無上的依賴注入機(jī)制。它提供了一個(gè)可注入彼此依賴下列核心組件。

工廠

服務(wù)

提供者

常值


值是簡(jiǎn)單的JavaScript對(duì)象,它是用來將值傳遞過程中的配置相位控制器。

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

工廠

工廠是用于返回函數(shù)的值。它根據(jù)需求創(chuàng)造值,每當(dāng)一個(gè)服務(wù)或控制器需要。它通常使用一個(gè)工廠函數(shù)來計(jì)算并返回對(duì)應(yīng)值

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

服務(wù)

服務(wù)是一個(gè)單一的JavaScript包含了一組函數(shù)對(duì)象來執(zhí)行某些任務(wù)。服務(wù)使用service()函數(shù),然后注入到控制器的定義。

//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內(nèi)部創(chuàng)建過程中配置階段的服務(wù),工廠等(相AngularJS引導(dǎo)自身期間)。下面提到的腳本,可以用來創(chuàng)建,我們已經(jīng)在前面創(chuàng)建MathService。提供者是一個(gè)特殊的工廠方法以及get()方法,用來返回值/服務(wù)/工廠。

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

常量

常量用于通過配置相位值考慮事實(shí),值不能使用期間的配置階段被傳遞。

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

例子

下面的例子將展示上述所有指令。

testAngularJS.html

<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" />
   <button ng-click="square()">X<sup>2</sup></button>
   <p>Result: {{result}}</p>
  </div>
  <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.2.15/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>

結(jié)果

在Web瀏覽器打開textAngularJS.html。看到結(jié)果如下。

以上就是對(duì)AngularJS 依賴注入的資料整理,后續(xù)繼續(xù)補(bǔ)充相關(guān)資料,謝謝大家對(duì)本站的支持!

相關(guān)文章

  • AngularJS $http模塊POST請(qǐng)求實(shí)現(xiàn)

    AngularJS $http模塊POST請(qǐng)求實(shí)現(xiàn)

    本篇文章主要介紹了AngularJS $http模塊POST請(qǐng)求實(shí)現(xiàn),這里整理了詳細(xì)的代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-04-04
  • angular異步驗(yàn)證防抖踩坑實(shí)錄

    angular異步驗(yàn)證防抖踩坑實(shí)錄

    這篇文章主要給大家介紹了關(guān)于angular異步驗(yàn)證防抖踩坑的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家學(xué)習(xí)或者使用angular具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧
    2019-12-12
  • 詳解為Angular.js內(nèi)置$http服務(wù)添加攔截器的方法

    詳解為Angular.js內(nèi)置$http服務(wù)添加攔截器的方法

    所謂攔截器就是在目標(biāo)達(dá)到目的地之前對(duì)其進(jìn)行處理以便處理結(jié)果更加符合我們的預(yù)期。Angular的$http攔截器是通過$httpProvider.interceptors數(shù)組定義的一組攔截器,每個(gè)攔截器都是實(shí)現(xiàn)了某些特定方法的Factory。本文就介紹了為Angular.js內(nèi)置$http服務(wù)添加攔截器的方法。
    2016-12-12
  • 基于AngularJs select綁定數(shù)字類型的問題

    基于AngularJs select綁定數(shù)字類型的問題

    今天小編就為大家分享一篇基于AngularJs select綁定數(shù)字類型的問題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧
    2018-10-10
  • Angular中的Promise對(duì)象($q介紹)

    Angular中的Promise對(duì)象($q介紹)

    這篇文章主要介紹了Angular中的Promise對(duì)象($q介紹),本文講解了Promise模式、Q Promise的基本用法、AngularJs中的$q.defferd等內(nèi)容,需要的朋友可以參考下
    2015-03-03
  • angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼

    angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼

    本篇文章主要介紹了angularjs2 ng2 密碼隱藏顯示的實(shí)例代碼,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2017-08-08
  • Angular實(shí)現(xiàn)圖片裁剪工具ngImgCrop實(shí)踐

    Angular實(shí)現(xiàn)圖片裁剪工具ngImgCrop實(shí)踐

    本篇文章主要介紹了Angular實(shí)現(xiàn)圖片裁剪工具ngImgCrop實(shí)踐,具有一定的參考價(jià)值,有興趣的可以了解一下
    2017-08-08
  • Angular中樣式綁定解決方案

    Angular中樣式綁定解決方案

    這篇文章主要介紹了Angular中樣式綁定解決方案,使用ngClass和ngStyle可以進(jìn)行樣式的綁定,本文結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2023-08-08
  • Angular.js實(shí)現(xiàn)掃碼槍掃碼并生成二維碼

    Angular.js實(shí)現(xiàn)掃碼槍掃碼并生成二維碼

    這篇文章主要為大家介紹了Angular.js實(shí)現(xiàn)掃碼槍掃碼并生成二維碼示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2023-03-03
  • AngularJS深入探討scope,繼承結(jié)構(gòu),事件系統(tǒng)和生命周期

    AngularJS深入探討scope,繼承結(jié)構(gòu),事件系統(tǒng)和生命周期

    這篇文章主要介紹了AngularJS的scope,繼承結(jié)構(gòu),事件系統(tǒng)和生命周期,較為詳細(xì)的分析了scope的作用域、層次結(jié)構(gòu)、繼承及生命周期相關(guān)概念與使用技巧,需要的朋友可以參考下
    2016-11-11

最新評(píng)論

平南县| 太保市| 芦山县| 铜山县| 娄底市| 罗江县| 阳西县| 南涧| 尼勒克县| 凤台县| 广元市| 余干县| 兴山县| 启东市| 韩城市| 米脂县| 蓝田县| 泾源县| 柳州市| 临桂县| 晋城| 高清| 仁布县| 托克逊县| 剑河县| 中卫市| 靖安县| 松江区| 琼结县| 宜城市| 鸡泽县| 新乡市| 石景山区| 泸溪县| 武功县| 牡丹江市| 阿坝| 长丰县| 霍城县| 金华市| 噶尔县|