fix(core): make injector.get() return default value with InjectFlags.Self flag on (#27739)

Fixes #27729

PR Close #27739
This commit is contained in:
thekiba
2019-04-10 14:42:41 +03:00
committed by Kara Erickson
parent e1065eec5b
commit 0477bfc8ed
3 changed files with 40 additions and 2 deletions

View File

@ -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;
}

View File

@ -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] || [];