2222 * The injector function also has these properties:
2323 *
2424 * * An `invoke` property which can be used to invoke methods with dependency-injected arguments.
25- * `injector.invoke(self, fn, curryArgs )`
25+ * `injector.invoke(self, fn, locals )`
2626 * * `self` - The "`this`" to be used when invoking the function.
2727 * * `fn` - The function to be invoked. The function may have the `$inject` property that
2828 * lists the set of arguments which should be auto-injected.
2929 * (see {@link guide/dev_guide.di dependency injection}).
30- * * `curryArgs (array)` - Optional array of arguments to pass to the function
31- * invocation after the injection arguments (also known as curry arguments or currying) .
30+ * * `locals (array)` - Optional array of arguments to pass to the function
31+ * invocation after the injection arguments.
3232 * * An `eager` property which is used to initialize the eager services.
3333 * `injector.eager()`
3434 */
35- function createInjector ( factories ) {
36- var instanceCache = {
37- $injector : injector
38- } ;
39- factories = factories || angularService ;
40-
41- injector . invoke = invoke ;
42-
43- forEach ( factories , function ( factory , name ) {
44- if ( factory . $eager )
45- injector ( name ) ;
46- } ) ;
47- return instanceCache . $injector ;
48-
49- function injector ( serviceId , path ) {
50- if ( typeof serviceId == 'string' ) {
51- if ( ! ( serviceId in instanceCache ) ) {
52- var factory = factories [ serviceId ] ;
53- path = path || [ ] ;
54- path . unshift ( serviceId ) ;
55- if ( ! factory ) throw Error ( "Unknown provider for '" + path . join ( "' <- '" ) + "'." ) ;
56- inferInjectionArgs ( factory ) ;
57- instanceCache [ serviceId ] = invoke ( null , factory , [ ] , path ) ;
58- path . shift ( ) ;
59- }
60- return instanceCache [ serviceId ] ;
61- } else {
62- return invoke ( null , serviceId , path ) ;
63- }
64- }
65-
66- function invoke ( self , fn , args , path ) {
67- args = args || [ ] ;
68- var injectNames ;
69- var i ;
70- if ( typeof fn == 'function' ) {
71- injectNames = fn . $inject || [ ] ;
72- i = injectNames . length ;
73- } else if ( fn instanceof Array ) {
74- injectNames = fn ;
75- i = injectNames . length ;
76- fn = injectNames [ -- i ] ;
77- }
78- assertArgFn ( fn , 'fn' ) ;
79- while ( i -- ) {
80- args . unshift ( injector ( injectNames [ i ] , path ) ) ;
81- }
82- return fn . apply ( self , args ) ;
83- }
84- }
85-
86- /**
87- * THIS IS NOT PUBLIC DOC YET!
88- *
89- * @name angular.annotate
90- * @function
91- *
92- * @description
93- * Annotate the function with injection arguments. This is equivalent to setting the `$inject`
94- * property as described in {@link guide.di dependency injection}.
95- *
96- * <pre>
97- * var MyController = angular.annotate('$location', function($location){ ... });
98- * </pre>
99- *
100- * is the same as
101- *
102- * <pre>
103- * var MyController = function($location){ ... };
104- * MyController.$inject = ['$location'];
105- * </pre>
106- *
107- * @param {String|Array } serviceName... zero or more service names to inject into the
108- * `annotatedFunction`.
109- * @param {function } annotatedFunction function to annotate with `$inject`
110- * functions.
111- * @returns {function } `annotatedFunction`
112- */
113- function annotate ( services , fn ) {
114- if ( services instanceof Array ) {
115- fn . $inject = services ;
116- return fn ;
117- } else {
118- var i = 0 ,
119- length = arguments . length - 1 , // last one is the destination function
120- $inject = arguments [ length ] . $inject = [ ] ;
121- for ( ; i < length ; i ++ ) {
122- $inject . push ( arguments [ i ] ) ;
123- }
124- return arguments [ length ] ; // return the last one
125- }
126- }
12735
12836function angularServiceInject ( name , fn , inject , eager ) {
12937 angularService ( name , fn , { $inject :inject , $eager :eager } ) ;
@@ -156,22 +64,16 @@ function inferInjectionArgs(fn) {
15664}
15765
15866///////////////////////////////////////
159- function createInjector2 ( modulesToLoad , moduleRegistry ) {
67+ function createInjector ( modulesToLoad , moduleRegistry ) {
16068 var cache = { } ,
16169 $injector = internalInjector ( cache ) ,
16270 providerSuffix = 'Provider' ,
16371 providerSuffixLength = providerSuffix . length ;
16472
165- function $provide ( name ) {
166- var provider = cache [ '#' + name + providerSuffix ] ;
167- if ( provider ) {
168- return provider ;
169- } else {
170- throw Error ( "No provider for: " + name ) ;
171- }
172- }
73+ value ( '$injector' , $injector ) ;
74+ value ( '$provide' , { service : service , factory : factory , value : value } ) ;
17375
174- $provide . service = function ( name , provider ) {
76+ function service ( name , provider ) {
17577 if ( isFunction ( provider ) ) {
17678 provider = $injector . instantiate ( provider ) ;
17779 }
@@ -180,11 +82,8 @@ function createInjector2(modulesToLoad, moduleRegistry) {
18082 }
18183 cache [ '#' + name + providerSuffix ] = provider ;
18284 } ;
183- $provide . factory = function ( name , factoryFn ) { $provide . service ( name , { $get :factoryFn } ) ; } ;
184- $provide . value = function ( name , value ) { $provide . factory ( name , valueFn ( value ) ) ; } ;
185-
186- $provide . value ( '$injector' , $injector ) ;
187- $provide . value ( '$provide' , $provide ) ;
85+ function factory ( name , factoryFn ) { service ( name , { $get :factoryFn } ) ; } ;
86+ function value ( name , value ) { factory ( name , valueFn ( value ) ) ; } ;
18887
18988 function internalInjector ( cache ) {
19089 var path = [ ] ;
@@ -194,9 +93,10 @@ function createInjector2(modulesToLoad, moduleRegistry) {
19493 case 'function' :
19594 return invoke ( null , value ) ;
19695 case 'string' :
197- var instanceKey = '#' + value ;
198- if ( cache [ instanceKey ] ) {
199- return cache [ instanceKey ] ;
96+ var instanceKey = '#' + value ,
97+ instance = cache [ instanceKey ] ;
98+ if ( instance !== undefined || cache . hasOwnProperty ( instanceKey ) ) {
99+ return instance ;
200100 }
201101 try {
202102 path . unshift ( value ) ;
@@ -219,28 +119,32 @@ function createInjector2(modulesToLoad, moduleRegistry) {
219119 }
220120 }
221121
222- function invoke ( self , fn ) {
122+ function invoke ( self , fn , locals ) {
223123 var args = [ ] ,
224124 $inject ,
225- length ;
226125
227- switch ( typeof fn ) {
228- case 'function' :
229- $inject = inferInjectionArgs ( fn ) ;
126+ length ,
127+ key ;
128+
129+ if ( fn instanceof Function ) {
130+ $inject = inferInjectionArgs ( fn ) ;
131+ length = $inject . length ;
132+ } else {
133+ if ( fn instanceof Array ) {
134+ $inject = fn ;
230135 length = $inject . length ;
231- break ;
232- case 'object' :
233- if ( typeof fn . length == 'number' ) {
234- $inject = fn ;
235- length = $inject . length ;
236- fn = $inject [ -- length ] ;
237- }
238- default :
239- assertArgFn ( fn , 'fn' ) ;
240- } ;
136+ fn = $inject [ -- length ] ;
137+ }
138+ assertArgFn ( fn , 'fn' ) ;
139+ }
241140
242141 while ( length -- ) {
243- args . unshift ( injector ( $inject [ length ] , path ) ) ;
142+ key = $inject [ length ] ;
143+ args . unshift (
144+ locals && locals . hasOwnProperty ( key )
145+ ? locals [ key ]
146+ : injector ( $inject [ length ] , path )
147+ ) ;
244148 }
245149
246150 switch ( self ? - 1 : args . length ) {
0 commit comments