Angularjs-dependency-injection
AngularJS-依存性注入
依存性注入は、コンポーネント内でハードコーディングするのではなく、コンポーネントに依存関係を与えるソフトウェア設計です。 コンポーネントが依存関係を見つけるのを防ぎ、依存関係を構成可能にします。 また、コンポーネントを再利用可能、保守可能、テスト可能にするのにも役立ちます。
AngularJSは、最高の依存性注入メカニズムを提供します。 依存関係として互いに注入できる次のコアコンポーネントを提供します。
- 値
- 工場
- サービス
- プロバイダ *定数
値
値は単純なJavaScriptオブジェクトです。これは、構成フェーズ中にコントローラーに値を渡すために必要です(構成フェーズは、AngularJSがブートストラップするときです)。
//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);
}
});
工場
ファクトリは、値を返すために使用される関数です。 サービスまたはコントローラーが必要とするたびに、オンデマンドで値を作成します。 通常、ファクトリ関数を使用して値を計算して返します。
//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オブジェクトです。 サービスはservice()関数を使用して定義され、コントローラーに挿入されます。
//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によって内部的に使用されます。 構成フェーズ中。 次のスクリプトを使用して、前に作成した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>
<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"/></p>
<button ng-click = "square()">X<sup>2</sup></button>
<p>Result: {{result}}</p>
</div>
<script src = "https://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/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ブラウザーで_testAngularJS_を開き、結果を確認します。