diff --git a/modules/@angular/core/src/view/provider.ts b/modules/@angular/core/src/view/provider.ts index 5dcdc8b1b0..9448c4d5af 100644 --- a/modules/@angular/core/src/view/provider.ts +++ b/modules/@angular/core/src/view/provider.ts @@ -220,7 +220,7 @@ export function resolveDep( if (elDef.parent != null) { elIndex = elDef.parent; } else { - elIndex = view.parentDiIndex; + elIndex = parentDiIndex(view); view = view.parent; } } @@ -246,12 +246,26 @@ export function resolveDep( return asProviderData(view, providerIndex).instance; } } - elIndex = view.parentDiIndex; + elIndex = parentDiIndex(view); view = view.parent; } return Injector.NULL.get(depDef.token, notFoundValue); } +/** + * for component views, this is the same as parentIndex. + * for embedded views, this is the index of the parent node + * that contains the view container. + */ +function parentDiIndex(view: ViewData): number { + if (view.parent) { + const parentNodeDef = view.def.nodes[view.parentIndex]; + return parentNodeDef.element && parentNodeDef.element.template ? parentNodeDef.parent : + parentNodeDef.index; + } + return view.parentIndex; +} + export function createInjector(view: ViewData, elIndex: number): Injector { return new Injector_(view, elIndex); } diff --git a/modules/@angular/core/src/view/types.ts b/modules/@angular/core/src/view/types.ts index 008193ead4..d8969de975 100644 --- a/modules/@angular/core/src/view/types.ts +++ b/modules/@angular/core/src/view/types.ts @@ -259,10 +259,6 @@ export interface ViewData { // index of parent element / anchor. Not the index // of the provider with the component view. parentIndex: number; - // for component views, this is the same as parentIndex. - // for embedded views, this is the index of the parent node - // that contains the view container. - parentDiIndex: number; parent: ViewData; component: any; context: any; diff --git a/modules/@angular/core/src/view/view.ts b/modules/@angular/core/src/view/view.ts index d1363bec0c..19006d6f2e 100644 --- a/modules/@angular/core/src/view/view.ts +++ b/modules/@angular/core/src/view/view.ts @@ -223,8 +223,7 @@ function cloneAndModifyElement( export function createEmbeddedView(parent: ViewData, anchorDef: NodeDef, context?: any): ViewData { // embedded views are seen as siblings to the anchor, so we need // to get the parent of the anchor and use it as parentIndex. - const view = createView( - parent.services, parent, anchorDef.index, anchorDef.parent, anchorDef.element.template); + const view = createView(parent.services, parent, anchorDef.index, anchorDef.element.template); initView(view, parent.component, context); createViewNodes(view); return view; @@ -236,15 +235,14 @@ export function createEmbeddedView(parent: ViewData, anchorDef: NodeDef, context */ export function createRootView( services: Services, defFactory: ViewDefinitionFactory, context?: any): ViewData { - const view = createView(services, null, null, null, resolveViewDefinition(defFactory)); + const view = createView(services, null, null, resolveViewDefinition(defFactory)); initView(view, context, context); createViewNodes(view); return view; } function createView( - services: Services, parent: ViewData, parentIndex: number, parentDiIndex: number, - def: ViewDefinition): ViewData { + services: Services, parent: ViewData, parentIndex: number, def: ViewDefinition): ViewData { const nodes: NodeData[] = new Array(def.nodes.length); let renderer: Renderer; if (def.flags != null && (def.flags & ViewFlags.DirectDom)) { @@ -257,7 +255,6 @@ function createView( def, parent, parentIndex, - parentDiIndex, context: undefined, component: undefined, nodes, state: ViewState.FirstCheck, renderer, services, @@ -303,8 +300,7 @@ function _createViewNodes(view: ViewData) { if (nodeDef.provider.component) { const hostElIndex = nodeDef.parent; componentView = createView( - view.services, view, hostElIndex, hostElIndex, - resolveViewDefinition(nodeDef.provider.component)); + view.services, view, hostElIndex, resolveViewDefinition(nodeDef.provider.component)); } const providerData = nodeData = createProvider(view, nodeDef, componentView); if (componentView) {