AngularJS 依賴注入詳解及示例代碼
依賴注入是一個在組件中給出的替代了硬的組件內(nèi)的編碼它們的依賴關系的軟件設計模式。這減輕一個組成部分,從定位的依賴,依賴配置。這有助于使組件可重用,維護和測試。
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); } });
工廠
工廠是用于返回函數(shù)的值。它根據(jù)需求創(chuàng)造值,每當一個服務或控制器需要。它通常使用一個工廠函數(shù)來計算并返回對應值
//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包含了一組函數(shù)對象來執(zhí)行某些任務。服務使用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)建過程中配置階段的服務,工廠等(相AngularJS引導自身期間)。下面提到的腳本,可以用來創(chuàng)建,我們已經(jīng)在前面創(chuàng)建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
<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>
結果
在Web瀏覽器打開textAngularJS.html??吹浇Y果如下。
以上就是對AngularJS 依賴注入的資料整理,后續(xù)繼續(xù)補充相關資料,謝謝大家對本站的支持!
相關文章
Ionic + Angular.js實現(xiàn)驗證碼倒計時功能的方法
驗證碼倒計時這個功能相信對大家每個人來說都不陌生,之前介紹了在Android中的實現(xiàn)方法,下面這篇文章主要給大家介紹了利用Ionic + Angular.js實現(xiàn)驗證碼倒計時功能的相關資料,文中介紹的非常詳細,需要的朋友們下面來一起看看吧。2017-06-06Angular2使用Guard和Resolve進行驗證和權限控制
本篇文章主要介紹了Angular2使用Guard和Resolve進行驗證和權限控制,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-04-04AngularJS使用angular-formly進行表單驗證
這篇文章主要介紹了AngularJS使用angular-formly進行表單驗證的相關資料,需要的朋友可以參考下2015-12-12AngularJS $on、$emit和$broadcast的使用
本文主要介紹AngularJS $on、$emit和$broadcast的使用,這里整理了詳細的資料及簡單示例代碼有興趣的小伙伴可以參考下2016-09-09