2011-06-06 15:50:35 +00:00
|
|
|
@ngdoc overview
|
|
|
|
|
@name Developer Guide: Angular Services: Injecting Services Into Controllers
|
|
|
|
|
@description
|
|
|
|
|
|
|
|
|
|
Using services as dependencies for controllers is very similar to using services as dependencies
|
|
|
|
|
for another service.
|
|
|
|
|
|
|
|
|
|
Since JavaScript is a dynamic language, DI can't figure out which services to inject by static
|
2012-03-13 21:23:38 +00:00
|
|
|
types (like in static typed languages). Therefore, you can specify the service name by using the
|
2011-06-06 15:50:35 +00:00
|
|
|
`$inject` property, which is an array containing strings with names of services to be injected.
|
|
|
|
|
The name must match the corresponding service ID registered with angular. The order of the service
|
|
|
|
|
IDs matters: the order of the services in the array will be used when calling the factory function
|
|
|
|
|
with injected parameters. The names of parameters in factory function don't matter, but by
|
2012-03-13 21:23:38 +00:00
|
|
|
convention they match the service IDs, which has added benefits discussed below.
|
2011-06-06 15:50:35 +00:00
|
|
|
|
|
|
|
|
<pre>
|
|
|
|
|
function myController($loc, $log) {
|
2013-10-30 14:15:41 +00:00
|
|
|
this.firstMethod = function() {
|
|
|
|
|
// use $location service
|
|
|
|
|
$loc.setHash();
|
|
|
|
|
};
|
|
|
|
|
this.secondMethod = function() {
|
|
|
|
|
// use $log service
|
|
|
|
|
$log.info('...');
|
|
|
|
|
};
|
2011-06-06 15:50:35 +00:00
|
|
|
}
|
|
|
|
|
// which services to inject ?
|
|
|
|
|
myController.$inject = ['$location', '$log'];
|
|
|
|
|
</pre>
|
|
|
|
|
|
2011-11-14 23:58:40 +00:00
|
|
|
<doc:example module="MyServiceModule">
|
2011-06-06 15:50:35 +00:00
|
|
|
<doc:source>
|
2012-04-29 05:45:28 +00:00
|
|
|
<script>
|
2012-01-12 19:06:10 +00:00
|
|
|
angular.
|
|
|
|
|
module('MyServiceModule', []).
|
|
|
|
|
factory('notify', ['$window', function(win) {
|
2011-11-09 01:40:52 +00:00
|
|
|
var msgs = [];
|
|
|
|
|
return function(msg) {
|
|
|
|
|
msgs.push(msg);
|
|
|
|
|
if (msgs.length == 3) {
|
|
|
|
|
win.alert(msgs.join("\n"));
|
|
|
|
|
msgs = [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}]);
|
2011-06-06 15:50:35 +00:00
|
|
|
|
2012-03-13 21:23:38 +00:00
|
|
|
function myController(scope, notifyService) {
|
|
|
|
|
scope.callNotify = function(msg) {
|
2011-06-06 15:50:35 +00:00
|
|
|
notifyService(msg);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-13 21:23:38 +00:00
|
|
|
myController.$inject = ['$scope','notify'];
|
2011-06-06 15:50:35 +00:00
|
|
|
</script>
|
|
|
|
|
|
2014-01-12 00:59:15 +00:00
|
|
|
<div id="simple" ng-controller="myController">
|
2012-03-13 21:23:38 +00:00
|
|
|
<p>Let's try this simple notify service, injected into the controller...</p>
|
|
|
|
|
<input ng-init="message='test'" ng-model="message" >
|
|
|
|
|
<button ng-click="callNotify(message);">NOTIFY</button>
|
2013-07-10 14:42:34 +00:00
|
|
|
<p>(you have to click 3 times to see an alert)</p>
|
2011-06-06 15:50:35 +00:00
|
|
|
</div>
|
|
|
|
|
</doc:source>
|
2014-01-12 00:59:15 +00:00
|
|
|
<doc:protractor>
|
2012-03-13 21:23:38 +00:00
|
|
|
it('should test service', function() {
|
2014-01-12 00:59:15 +00:00
|
|
|
expect(element(by.id('simple')).element(by.model('message')).getAttribute('value'))
|
|
|
|
|
.toEqual('test');
|
2012-03-13 21:23:38 +00:00
|
|
|
});
|
2014-01-12 00:59:15 +00:00
|
|
|
</doc:protractor>
|
2011-06-06 15:50:35 +00:00
|
|
|
</doc:example>
|
|
|
|
|
|
2012-03-13 21:23:38 +00:00
|
|
|
## Implicit Dependency Injection
|
|
|
|
|
|
|
|
|
|
A new feature of Angular DI allows it to determine the dependency from the name of the parameter.
|
|
|
|
|
Let's rewrite the above example to show the use of this implicit dependency injection of
|
|
|
|
|
`$window`, `$scope`, and our `notify` service:
|
|
|
|
|
|
|
|
|
|
<doc:example module="MyServiceModuleDI">
|
|
|
|
|
<doc:source>
|
2012-04-29 05:45:28 +00:00
|
|
|
<script>
|
2012-03-13 21:23:38 +00:00
|
|
|
angular.
|
|
|
|
|
module('MyServiceModuleDI', []).
|
|
|
|
|
factory('notify', function($window) {
|
|
|
|
|
var msgs = [];
|
|
|
|
|
return function(msg) {
|
|
|
|
|
msgs.push(msg);
|
|
|
|
|
if (msgs.length == 3) {
|
|
|
|
|
$window.alert(msgs.join("\n"));
|
|
|
|
|
msgs = [];
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
function myController($scope, notify) {
|
|
|
|
|
$scope.callNotify = function(msg) {
|
|
|
|
|
notify(msg);
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
</script>
|
2014-01-12 00:59:15 +00:00
|
|
|
<div id="implicit" ng-controller="myController">
|
2012-03-13 21:23:38 +00:00
|
|
|
<p>Let's try the notify service, that is implicitly injected into the controller...</p>
|
|
|
|
|
<input ng-init="message='test'" ng-model="message">
|
|
|
|
|
<button ng-click="callNotify(message);">NOTIFY</button>
|
2013-06-04 21:24:49 +00:00
|
|
|
<p>(you have to click 3 times to see an alert)</p>
|
2012-03-13 21:23:38 +00:00
|
|
|
</div>
|
|
|
|
|
</doc:source>
|
|
|
|
|
</doc:example>
|
|
|
|
|
|
|
|
|
|
However, if you plan to {@link http://en.wikipedia.org/wiki/Minification_(programming) minify} your
|
|
|
|
|
code, your variable names will get renamed in which case you will still need to explicitly specify
|
|
|
|
|
dependencies with the `$inject` property.
|
2011-06-06 15:50:35 +00:00
|
|
|
|
|
|
|
|
## Related Topics
|
|
|
|
|
|
|
|
|
|
{@link dev_guide.services.understanding_services Understanding Angular Services}
|
|
|
|
|
{@link dev_guide.services.creating_services Creating Angular Services}
|
|
|
|
|
{@link dev_guide.services.managing_dependencies Managing Service Dependencies}
|
|
|
|
|
{@link dev_guide.services.testing_services Testing Angular Services}
|
|
|
|
|
|
|
|
|
|
## Related API
|
|
|
|
|
|
2012-06-12 06:49:24 +00:00
|
|
|
{@link api/ng Angular Service API}
|