File tree Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Expand file tree Collapse file tree 2 files changed +17
-0
lines changed Original file line number Diff line number Diff line change @@ -851,6 +851,10 @@ export class ElementInjector extends TreeNode {
851851 }
852852 if ( dep . key . id === StaticKeys . instance ( ) . protoViewId ) {
853853 if ( isBlank ( this . _preBuiltObjects . protoView ) ) {
854+ if ( dep . optional ) {
855+ return null ;
856+ }
857+
854858 throw new NoBindingError ( dep . key ) ;
855859 }
856860 return new ProtoViewRef ( this . _preBuiltObjects . protoView ) ;
Original file line number Diff line number Diff line change @@ -150,6 +150,13 @@ class NeedsProtoViewRef {
150150 }
151151}
152152
153+ class OptionallyInjectsProtoViewRef {
154+ protoViewRef ;
155+ constructor ( @Optional ( ) ref :ProtoViewRef ) {
156+ this . protoViewRef = ref ;
157+ }
158+ }
159+
153160class NeedsChangeDetectorRef {
154161 changeDetectorRef ;
155162 constructor ( cdr :ChangeDetectorRef ) {
@@ -811,6 +818,12 @@ export function main() {
811818 ( ) => injector ( [ NeedsProtoViewRef ] )
812819 ) . toThrowError ( 'No provider for ProtoViewRef! (NeedsProtoViewRef -> ProtoViewRef)' ) ;
813820 } ) ;
821+
822+ it ( 'should inject null if there is no ProtoViewRef when the dependency is optional' , ( ) => {
823+ var inj = injector ( [ OptionallyInjectsProtoViewRef ] ) ;
824+ var instance = inj . get ( OptionallyInjectsProtoViewRef ) ;
825+ expect ( instance . protoViewRef ) . toBeNull ( ) ;
826+ } ) ;
814827 } ) ;
815828
816829 describe ( 'directive queries' , ( ) => {
You can’t perform that action at this time.
0 commit comments