feat(element_injector): allow @Optional for ProtoViewRef
This commit is contained in:
parent
0114cd97b6
commit
bb2eda2d15
|
@ -851,6 +851,10 @@ export class ElementInjector extends TreeNode {
|
||||||
}
|
}
|
||||||
if (dep.key.id === StaticKeys.instance().protoViewId) {
|
if (dep.key.id === StaticKeys.instance().protoViewId) {
|
||||||
if (isBlank(this._preBuiltObjects.protoView)) {
|
if (isBlank(this._preBuiltObjects.protoView)) {
|
||||||
|
if (dep.optional) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
throw new NoBindingError(dep.key);
|
throw new NoBindingError(dep.key);
|
||||||
}
|
}
|
||||||
return new ProtoViewRef(this._preBuiltObjects.protoView);
|
return new ProtoViewRef(this._preBuiltObjects.protoView);
|
||||||
|
|
|
@ -150,6 +150,13 @@ class NeedsProtoViewRef {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class OptionallyInjectsProtoViewRef {
|
||||||
|
protoViewRef;
|
||||||
|
constructor(@Optional() ref:ProtoViewRef) {
|
||||||
|
this.protoViewRef = ref;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class NeedsChangeDetectorRef {
|
class NeedsChangeDetectorRef {
|
||||||
changeDetectorRef;
|
changeDetectorRef;
|
||||||
constructor(cdr:ChangeDetectorRef) {
|
constructor(cdr:ChangeDetectorRef) {
|
||||||
|
@ -811,6 +818,12 @@ export function main() {
|
||||||
() => injector([NeedsProtoViewRef])
|
() => injector([NeedsProtoViewRef])
|
||||||
).toThrowError('No provider for ProtoViewRef! (NeedsProtoViewRef -> ProtoViewRef)');
|
).toThrowError('No provider for ProtoViewRef! (NeedsProtoViewRef -> ProtoViewRef)');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should inject null if there is no ProtoViewRef when the dependency is optional', () => {
|
||||||
|
var inj = injector([OptionallyInjectsProtoViewRef]);
|
||||||
|
var instance = inj.get(OptionallyInjectsProtoViewRef);
|
||||||
|
expect(instance.protoViewRef).toBeNull();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('directive queries', () => {
|
describe('directive queries', () => {
|
||||||
|
|
Loading…
Reference in New Issue