亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

AngularJS 依賴(lài)注入詳解及示例代碼

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

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

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

工廠

服務(wù)

提供者

常值


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

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

常量

常量用于通過(guò)配置相位值考慮事實(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瀏覽器打開(kāi)textAngularJS.html??吹浇Y(jié)果如下。

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

相關(guān)文章

  • AngularJS 防止頁(yè)面閃爍的方法

    AngularJS 防止頁(yè)面閃爍的方法

    我們知道在應(yīng)用的頁(yè)面或者組件需要加載數(shù)據(jù)時(shí),瀏覽器和angular渲染頁(yè)面都需要消耗一定的時(shí)間。這篇文章主要介紹了AngularJS 防止頁(yè)面閃爍的方法,需要的朋友可以參考下
    2017-03-03
  • 詳解angular中使用echarts地圖

    詳解angular中使用echarts地圖

    這篇文章主要為大家介紹了angular中使用echarts地圖,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • 詳解AngularJS過(guò)濾器的使用

    詳解AngularJS過(guò)濾器的使用

    這篇文章主要為大家詳細(xì)介紹了AngularJS過(guò)濾器的使用方法,感興趣的小伙伴們可以參考一下
    2016-03-03
  • 詳細(xì)介紹RxJS在Angular中的應(yīng)用

    詳細(xì)介紹RxJS在Angular中的應(yīng)用

    本篇文章主要介紹了詳細(xì)介紹RxJS在Angular中的應(yīng)用,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-09-09
  • angularjs通過(guò)過(guò)濾器返回超鏈接的方法

    angularjs通過(guò)過(guò)濾器返回超鏈接的方法

    這篇文章主要介紹了angularjs通過(guò)過(guò)濾器返回超鏈接的方法,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2018-10-10
  • Angularjs 實(shí)現(xiàn)一個(gè)幻燈片示例代碼

    Angularjs 實(shí)現(xiàn)一個(gè)幻燈片示例代碼

    本文主要介紹Angularjs 寫(xiě)一個(gè)幻燈片的知識(shí),這里整理了詳細(xì)的資料,及實(shí)現(xiàn)代碼和實(shí)現(xiàn)效果圖有需要的小伙伴可以參考下
    2016-09-09
  • Ionic + Angular.js實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能的方法

    Ionic + Angular.js實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能的方法

    驗(yàn)證碼倒計(jì)時(shí)這個(gè)功能相信對(duì)大家每個(gè)人來(lái)說(shuō)都不陌生,之前介紹了在Android中的實(shí)現(xiàn)方法,下面這篇文章主要給大家介紹了利用Ionic + Angular.js實(shí)現(xiàn)驗(yàn)證碼倒計(jì)時(shí)功能的相關(guān)資料,文中介紹的非常詳細(xì),需要的朋友們下面來(lái)一起看看吧。
    2017-06-06
  • Angular2使用Guard和Resolve進(jìn)行驗(yàn)證和權(quán)限控制

    Angular2使用Guard和Resolve進(jìn)行驗(yàn)證和權(quán)限控制

    本篇文章主要介紹了Angular2使用Guard和Resolve進(jìn)行驗(yàn)證和權(quán)限控制,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧
    2017-04-04
  • AngularJS使用angular-formly進(jìn)行表單驗(yàn)證

    AngularJS使用angular-formly進(jìn)行表單驗(yàn)證

    這篇文章主要介紹了AngularJS使用angular-formly進(jìn)行表單驗(yàn)證的相關(guān)資料,需要的朋友可以參考下
    2015-12-12
  • AngularJS  $on、$emit和$broadcast的使用

    AngularJS $on、$emit和$broadcast的使用

    本文主要介紹AngularJS $on、$emit和$broadcast的使用,這里整理了詳細(xì)的資料及簡(jiǎn)單示例代碼有興趣的小伙伴可以參考下
    2016-09-09

最新評(píng)論