fix(core): make injector.get() return default value with InjectFlags.Self flag on (#27739)
Fixes #27729 PR Close #27739
This commit is contained in:
@ -342,6 +342,10 @@ function resolveToken(
|
||||
}
|
||||
} else if (!(flags & InjectFlags.Self)) {
|
||||
value = parent.get(token, notFoundValue, InjectFlags.Default);
|
||||
} else if (!(flags & InjectFlags.Optional)) {
|
||||
value = Injector.NULL.get(token, notFoundValue);
|
||||
} else {
|
||||
value = Injector.NULL.get(token, typeof notFoundValue !== 'undefined' ? notFoundValue : null);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
@ -197,7 +197,12 @@ export class R3Injector {
|
||||
// Select the next injector based on the Self flag - if self is set, the next injector is
|
||||
// the NullInjector, otherwise it's the parent.
|
||||
const nextInjector = !(flags & InjectFlags.Self) ? this.parent : getNullInjector();
|
||||
return nextInjector.get(token, flags & InjectFlags.Optional ? null : notFoundValue);
|
||||
// Set the notFoundValue based on the Optional flag - if optional is set and notFoundValue
|
||||
// is undefined, the value is null, otherwise it's the notFoundValue.
|
||||
notFoundValue = (flags & InjectFlags.Optional) && notFoundValue === THROW_IF_NOT_FOUND ?
|
||||
null :
|
||||
notFoundValue;
|
||||
return nextInjector.get(token, notFoundValue);
|
||||
} catch (e) {
|
||||
if (e.name === 'NullInjectorError') {
|
||||
const path: any[] = e[NG_TEMP_TOKEN_PATH] = e[NG_TEMP_TOKEN_PATH] || [];
|
||||
|
Reference in New Issue
Block a user