国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > JavaScript > 正文

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

2019-11-20 09:13:10
字體:
供稿:網(wǎng)友

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

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

工廠

服務(wù)

提供者

常值


值是簡單的JavaScript對象,它是用來將值傳遞過程中的配置相位控制器。

//define a modulevar 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)造值,每當一個服務(wù)或控制器需要。它通常使用一個工廠函數(shù)來計算并返回對應(yīng)值

//define a modulevar mainApp = angular.module("mainApp", []);//create a factory "MathService" which provides a method multiply to return multiplication of two numbersmainApp.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ù)是一個單一的JavaScript包含了一組函數(shù)對象來執(zhí)行某些任務(wù)。服務(wù)使用service()函數(shù),然后注入到控制器的定義。

//define a modulevar 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 controllermainApp.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。提供者是一個特殊的工廠方法以及get()方法,用來返回值/服務(wù)/工廠。

//define a modulevar 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

<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é)果如下。

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

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 上思县| 武宁县| 岳阳市| 广平县| 南溪县| 普安县| 五家渠市| 治多县| 炎陵县| 淄博市| 南涧| 乌恰县| 双江| 苍南县| 新民市| 周宁县| 彰化县| 来宾市| 柘荣县| 澳门| 磐安县| 札达县| 丘北县| 长春市| 福泉市| 平利县| 巴马| 长沙县| 富民县| 南江县| 黑水县| 文登市| 正阳县| 五莲县| 玛多县| 鸡东县| 南溪县| 合江县| 西青区| 新巴尔虎左旗| 北海市|