compileProvider是如何调用的
compileProvider定义
1 | //为了在injectionArgs方法中 annotate中返回 然后给injectionArgs 使用 |
我们回到publishExternalAPI函数看compileProvider的调用:
publishExternalAPI
1 | //instanceInjector 调用invoke函数 |
$provide.provider
实例化provider1
2
3
4
5
6
7
8
9
10
11
12
13function provider(name, provider_) {
assertNotHasOwnProperty(name, 'service');
if (isFunction(provider_) || isArray(provider_)) {
//通过providerInjector实例化compileProvider
//所以是从providerCache中获取参数
provider_ = providerInjector.instantiate(provider_);
}
//没有定义$get函数会报错
if (!provider_.$get) {
throw $injectorMinErr('pget', "Provider '{0}' must define $get factory method.", name);
}
return providerCache[name + providerSuffix] = provider_;
}
instantiate
$provide.Provider的时候会调用此方法实例化provider,比如CompileProvider1
2
3
4
5
6
7
8
9
10function instantiate(Type, locals, serviceName) {
// Check if Type is annotated and use just the given function at n-1 as parameter
// e.g. someModule.factory('greeter', ['$window', function(renamed$window) {}]);
var ctor = (isArray(Type) ? Type[Type.length - 1] : Type);
//注入参数 从providerCache中获取参数
var args = injectionArgs(Type, locals, serviceName);
// Empty object at position 0 is ignored for invocation with `new`, but required.
args.unshift(null);
return new (Function.prototype.bind.apply(ctor, args))();
}
instanceInjector.invoke
1 | ```js |