Skip to content

Commit 0bfaa57

Browse files
committed
feat($provide.service): Add $provide.service() for registering a class
1 parent 00d4427 commit 0bfaa57

File tree

4 files changed

+69
-3
lines changed

4 files changed

+69
-3
lines changed

src/Injector.js

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,13 +230,27 @@ function inferInjectionArgs(fn) {
230230
*
231231
* A short hand for configuring services if only `$get` method is required.
232232
*
233-
* @param {string} name The name of the instance. NOTE: the provider will be available under `name + 'Provider'` key.
233+
* @param {string} name The name of the instance.
234234
* @param {function()} $getFn The $getFn for the instance creation. Internally this is a short hand for
235235
* `$provide.provider(name, {$get: $getFn})`.
236236
* @returns {Object} registered provider instance
237237
*/
238238

239239

240+
/**
241+
* @ngdoc method
242+
* @name angular.module.AUTO.$provide#service
243+
* @methodOf angular.module.AUTO.$provide
244+
* @description
245+
*
246+
* A short hand for registering service of given class.
247+
*
248+
* @param {string} name The name of the instance.
249+
* @param {Function} constructor A class (constructor function) that will be instantiated.
250+
* @returns {Object} registered provider instance
251+
*/
252+
253+
240254
/**
241255
* @ngdoc method
242256
* @name angular.module.AUTO.$provide#value
@@ -245,7 +259,7 @@ function inferInjectionArgs(fn) {
245259
*
246260
* A short hand for configuring services if the `$get` method is a constant.
247261
*
248-
* @param {string} name The name of the instance. NOTE: the provider will be available under `name + 'Provider'` key.
262+
* @param {string} name The name of the instance.
249263
* @param {*} value The value.
250264
* @returns {Object} registered provider instance
251265
*/
@@ -296,6 +310,7 @@ function createInjector(modulesToLoad) {
296310
$provide: {
297311
provider: supportObject(provider),
298312
factory: supportObject(factory),
313+
service: supportObject(service),
299314
value: supportObject(value),
300315
constant: supportObject(constant),
301316
decorator: decorator
@@ -342,6 +357,12 @@ function createInjector(modulesToLoad) {
342357

343358
function factory(name, factoryFn) { return provider(name, { $get: factoryFn }); }
344359

360+
function service(name, constructor) {
361+
return factory(name, ['$injector', function($injector) {
362+
return $injector.instantiate(constructor);
363+
}]);
364+
}
365+
345366
function value(name, value) { return factory(name, valueFn(value)); }
346367

347368
function constant(name, value) {

src/loader.js

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,10 +125,21 @@ function setupModuleLoader(window) {
125125
* @param {string} name service name
126126
* @param {Function} providerFunction Function for creating new instance of the service.
127127
* @description
128-
* See {@link angular.module.AUTO.$provide#service $provide.factory()}.
128+
* See {@link angular.module.AUTO.$provide#factory $provide.factory()}.
129129
*/
130130
factory: invokeLater('$provide', 'factory'),
131131

132+
/**
133+
* @ngdoc method
134+
* @name angular.Module#service
135+
* @methodOf angular.Module
136+
* @param {string} name service name
137+
* @param {Function} constructor A constructor function that will be instantiated.
138+
* @description
139+
* See {@link angular.module.AUTO.$provide#service $provide.service()}.
140+
*/
141+
service: invokeLater('$provide', 'service'),
142+
132143
/**
133144
* @ngdoc method
134145
* @name angular.Module#value

test/InjectorSpec.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,6 +315,38 @@ describe('injector', function() {
315315
});
316316

317317

318+
describe('service', function() {
319+
it('should register a class', function() {
320+
var Type = function(value) {
321+
this.value = value;
322+
};
323+
324+
var instance = createInjector([function($provide) {
325+
$provide.value('value', 123);
326+
$provide.service('foo', Type);
327+
}]).get('foo');
328+
329+
expect(instance instanceof Type).toBe(true);
330+
expect(instance.value).toBe(123);
331+
});
332+
333+
334+
it('should register a set of classes', function() {
335+
var Type = function() {};
336+
337+
var injector = createInjector([function($provide) {
338+
$provide.service({
339+
foo: Type,
340+
bar: Type
341+
});
342+
}]);
343+
344+
expect(injector.get('foo') instanceof Type).toBe(true);
345+
expect(injector.get('bar') instanceof Type).toBe(true);
346+
});
347+
});
348+
349+
318350
describe('provider', function() {
319351
it('should configure $provide provider object', function() {
320352
expect(createInjector([function($provide) {

test/loaderSpec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ describe('module loader', function() {
3434
expect(myModule.
3535
provider('sk', 'sv').
3636
factory('fk', 'fv').
37+
service('a', 'aa').
3738
value('k', 'v').
3839
filter('f', 'ff').
3940
directive('d', 'dd').
@@ -47,6 +48,7 @@ describe('module loader', function() {
4748
['$injector', 'invoke', ['config'] ],
4849
['$provide', 'provider', ['sk', 'sv'] ],
4950
['$provide', 'factory', ['fk', 'fv'] ],
51+
['$provide', 'service', ['a', 'aa'] ],
5052
['$provide', 'value', ['k', 'v'] ],
5153
['$filterProvider', 'register', ['f', 'ff'] ],
5254
['$compileProvider', 'directive', ['d', 'dd'] ],

0 commit comments

Comments
 (0)