@@ -506,6 +506,7 @@ export class ElementInjector extends TreeNode {
506506 _query0 : QueryRef ;
507507 _query1 : QueryRef ;
508508 _query2 : QueryRef ;
509+
509510 constructor ( proto :ProtoElementInjector , parent :ElementInjector ) {
510511 super ( parent ) ;
511512 this . _proto = proto ;
@@ -568,7 +569,14 @@ export class ElementInjector extends TreeNode {
568569 this . _constructionCounter = 0 ;
569570 }
570571
571- instantiateDirectives ( lightDomAppInjector :Injector , host :ElementInjector , shadowDomAppInjector :Injector , preBuiltObjects :PreBuiltObjects ) {
572+ instantiateDirectives (
573+ lightDomAppInjector :Injector ,
574+ host :ElementInjector ,
575+ preBuiltObjects :PreBuiltObjects ) {
576+ var shadowDomAppInjector = null ;
577+ if ( this . _proto . _binding0IsComponent ) {
578+ shadowDomAppInjector = this . _createShadowDomAppInjector ( this . _proto . _binding0 , lightDomAppInjector ) ;
579+ }
572580 this . _host = host ;
573581 this . _checkShadowDomAppInjector ( shadowDomAppInjector ) ;
574582
@@ -578,6 +586,21 @@ export class ElementInjector extends TreeNode {
578586
579587 var p = this . _proto ;
580588 if ( isPresent ( p . _keyId0 ) ) this . _getDirectiveByKeyId ( p . _keyId0 ) ;
589+ if ( isPresent ( shadowDomAppInjector ) ) {
590+ var componentAnnotation :Component = this . _proto . _binding0 . annotation ;
591+ var publishAs = componentAnnotation . publishAs ;
592+ if ( isPresent ( publishAs ) && publishAs . length > 0 ) {
593+ // If there's a component directive on this element injector, then
594+ // 0-th key must contain the directive itself.
595+ // TODO(yjbanov): need to make injector creation faster:
596+ // - remove flattening of bindings array
597+ // - precalc token key
598+ this . _shadowDomAppInjector = shadowDomAppInjector . resolveAndCreateChild (
599+ ListWrapper . map ( publishAs , ( token ) => {
600+ return bind ( token ) . toValue ( this . getComponent ( ) ) ;
601+ } ) ) ;
602+ }
603+ }
581604 if ( isPresent ( p . _keyId1 ) ) this . _getDirectiveByKeyId ( p . _keyId1 ) ;
582605 if ( isPresent ( p . _keyId2 ) ) this . _getDirectiveByKeyId ( p . _keyId2 ) ;
583606 if ( isPresent ( p . _keyId3 ) ) this . _getDirectiveByKeyId ( p . _keyId3 ) ;
@@ -589,9 +612,22 @@ export class ElementInjector extends TreeNode {
589612 if ( isPresent ( p . _keyId9 ) ) this . _getDirectiveByKeyId ( p . _keyId9 ) ;
590613 }
591614
592- dynamicallyCreateComponent ( directiveBinding , injector :Injector ) {
593- this . _shadowDomAppInjector = injector ;
594- this . _dynamicallyCreatedComponentBinding = directiveBinding ;
615+ _createShadowDomAppInjector ( componentDirective :DirectiveBinding , appInjector :Injector ) {
616+ var shadowDomAppInjector = null ;
617+
618+ // shadowDomAppInjector
619+ var injectables = componentDirective . resolvedInjectables ;
620+ if ( isPresent ( injectables ) ) {
621+ shadowDomAppInjector = appInjector . createChildFromResolved ( injectables ) ;
622+ } else {
623+ shadowDomAppInjector = appInjector ;
624+ }
625+ return shadowDomAppInjector ;
626+ }
627+
628+ dynamicallyCreateComponent ( componentDirective :DirectiveBinding , parentInjector :Injector ) {
629+ this . _shadowDomAppInjector = this . _createShadowDomAppInjector ( componentDirective , parentInjector ) ;
630+ this . _dynamicallyCreatedComponentBinding = componentDirective ;
595631 this . _dynamicallyCreatedComponent = this . _new ( this . _dynamicallyCreatedComponentBinding ) ;
596632 return this . _dynamicallyCreatedComponent ;
597633 }
0 commit comments