diff options
| author | Vojta Jina | 2012-03-08 16:53:15 -0800 |
|---|---|---|
| committer | Vojta Jina | 2012-03-09 10:10:29 -0800 |
| commit | 0bfaa579c04d1b7cd21fbe16bfbc47a684f223b3 (patch) | |
| tree | 1e6c2147eed623ea9d17570511df1586a0af8449 /src | |
| parent | 00d4427388eeec81d434f9ee96bb7ccc70190923 (diff) | |
| download | angular.js-0bfaa579c04d1b7cd21fbe16bfbc47a684f223b3.tar.bz2 | |
feat($provide.service): Add $provide.service() for registering a class
Diffstat (limited to 'src')
| -rw-r--r-- | src/Injector.js | 25 | ||||
| -rw-r--r-- | src/loader.js | 13 |
2 files changed, 35 insertions, 3 deletions
diff --git a/src/Injector.js b/src/Injector.js index dc37a97c..10072959 100644 --- a/src/Injector.js +++ b/src/Injector.js @@ -230,7 +230,7 @@ function inferInjectionArgs(fn) { * * A short hand for configuring services if only `$get` method is required. * - * @param {string} name The name of the instance. NOTE: the provider will be available under `name + 'Provider'` key. + * @param {string} name The name of the instance. * @param {function()} $getFn The $getFn for the instance creation. Internally this is a short hand for * `$provide.provider(name, {$get: $getFn})`. * @returns {Object} registered provider instance @@ -239,13 +239,27 @@ function inferInjectionArgs(fn) { /** * @ngdoc method + * @name angular.module.AUTO.$provide#service + * @methodOf angular.module.AUTO.$provide + * @description + * + * A short hand for registering service of given class. + * + * @param {string} name The name of the instance. + * @param {Function} constructor A class (constructor function) that will be instantiated. + * @returns {Object} registered provider instance + */ + + +/** + * @ngdoc method * @name angular.module.AUTO.$provide#value * @methodOf angular.module.AUTO.$provide * @description * * A short hand for configuring services if the `$get` method is a constant. * - * @param {string} name The name of the instance. NOTE: the provider will be available under `name + 'Provider'` key. + * @param {string} name The name of the instance. * @param {*} value The value. * @returns {Object} registered provider instance */ @@ -296,6 +310,7 @@ function createInjector(modulesToLoad) { $provide: { provider: supportObject(provider), factory: supportObject(factory), + service: supportObject(service), value: supportObject(value), constant: supportObject(constant), decorator: decorator @@ -342,6 +357,12 @@ function createInjector(modulesToLoad) { function factory(name, factoryFn) { return provider(name, { $get: factoryFn }); } + function service(name, constructor) { + return factory(name, ['$injector', function($injector) { + return $injector.instantiate(constructor); + }]); + } + function value(name, value) { return factory(name, valueFn(value)); } function constant(name, value) { diff --git a/src/loader.js b/src/loader.js index 3e5bffef..e5c5215a 100644 --- a/src/loader.js +++ b/src/loader.js @@ -125,12 +125,23 @@ function setupModuleLoader(window) { * @param {string} name service name * @param {Function} providerFunction Function for creating new instance of the service. * @description - * See {@link angular.module.AUTO.$provide#service $provide.factory()}. + * See {@link angular.module.AUTO.$provide#factory $provide.factory()}. */ factory: invokeLater('$provide', 'factory'), /** * @ngdoc method + * @name angular.Module#service + * @methodOf angular.Module + * @param {string} name service name + * @param {Function} constructor A constructor function that will be instantiated. + * @description + * See {@link angular.module.AUTO.$provide#service $provide.service()}. + */ + service: invokeLater('$provide', 'service'), + + /** + * @ngdoc method * @name angular.Module#value * @methodOf angular.Module * @param {string} name service name |
