Skip to content

Commit d8e2795

Browse files
committed
fix(view): local variables override local variables set by ng-for
1 parent 7a41b19 commit d8e2795

File tree

2 files changed

+39
-20
lines changed

2 files changed

+39
-20
lines changed

modules/angular2/src/core/compiler/proto_view_factory.ts

Lines changed: 14 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -143,8 +143,8 @@ export class ProtoViewFactory {
143143
ListWrapper.map(allDirectives, directiveBinding => directiveBinding.metadata);
144144
var nestedPvsWithIndex = _collectNestedProtoViews(rootRenderProtoView);
145145
var nestedPvVariableBindings = _collectNestedProtoViewsVariableBindings(nestedPvsWithIndex);
146-
var nestedPvVariableNames =
147-
_collectNestedProtoViewsVariableNames(nestedPvsWithIndex, nestedPvVariableBindings);
146+
var nestedPvVariableNames = _collectNestedProtoViewsVariableNames(nestedPvsWithIndex);
147+
148148
var changeDetectorDefs =
149149
_getChangeDetectorDefinitions(hostComponentBinding.metadata, nestedPvsWithIndex,
150150
nestedPvVariableNames, allRenderDirectiveMetadata);
@@ -174,10 +174,7 @@ export function getChangeDetectorDefinitions(
174174
hostComponentMetadata: renderApi.DirectiveMetadata, rootRenderProtoView: renderApi.ProtoViewDto,
175175
allRenderDirectiveMetadata: List<renderApi.DirectiveMetadata>): List<ChangeDetectorDefinition> {
176176
var nestedPvsWithIndex = _collectNestedProtoViews(rootRenderProtoView);
177-
var nestedPvVariableBindings = _collectNestedProtoViewsVariableBindings(nestedPvsWithIndex);
178-
var nestedPvVariableNames =
179-
_collectNestedProtoViewsVariableNames(nestedPvsWithIndex, nestedPvVariableBindings);
180-
177+
var nestedPvVariableNames = _collectNestedProtoViewsVariableNames(nestedPvsWithIndex);
181178
return _getChangeDetectorDefinitions(hostComponentMetadata, nestedPvsWithIndex,
182179
nestedPvVariableNames, allRenderDirectiveMetadata);
183180
}
@@ -235,8 +232,6 @@ function _createAppProtoView(
235232
variableBindings: Map<string, string>, allDirectives: List<DirectiveBinding>): AppProtoView {
236233
var elementBinders = renderProtoView.elementBinders;
237234
var protoView = new AppProtoView(renderProtoView.render, protoChangeDetector, variableBindings);
238-
239-
// TODO: vsavkin refactor to pass element binders into proto view
240235
_createElementBinders(protoView, elementBinders, allDirectives);
241236
_bindDirectiveEvents(protoView, elementBinders);
242237

@@ -255,31 +250,30 @@ function _createVariableBindings(renderProtoView): Map<string, string> {
255250
MapWrapper.forEach(renderProtoView.variableBindings, (mappedName, varName) => {
256251
MapWrapper.set(variableBindings, varName, mappedName);
257252
});
258-
ListWrapper.forEach(renderProtoView.elementBinders, binder => {
259-
MapWrapper.forEach(binder.variableBindings, (mappedName, varName) => {
260-
MapWrapper.set(variableBindings, varName, mappedName);
261-
});
262-
});
263253
return variableBindings;
264254
}
265255

266256
function _collectNestedProtoViewsVariableNames(
267-
nestedPvsWithIndex: List<RenderProtoViewWithIndex>,
268-
nestedPvVariableBindings: List<Map<string, string>>): List<List<string>> {
257+
nestedPvsWithIndex: List<RenderProtoViewWithIndex>): List<List<string>> {
269258
var nestedPvVariableNames = ListWrapper.createFixedSize(nestedPvsWithIndex.length);
270259
ListWrapper.forEach(nestedPvsWithIndex, (pvWithIndex) => {
271260
var parentVariableNames =
272261
isPresent(pvWithIndex.parentIndex) ? nestedPvVariableNames[pvWithIndex.parentIndex] : null;
273262
nestedPvVariableNames[pvWithIndex.index] =
274-
_createVariableNames(parentVariableNames, nestedPvVariableBindings[pvWithIndex.index]);
263+
_createVariableNames(parentVariableNames, pvWithIndex.renderProtoView);
275264
});
276265
return nestedPvVariableNames;
277266
}
278267

279-
function _createVariableNames(parentVariableNames, variableBindings): List<string> {
280-
var variableNames = isPresent(parentVariableNames) ? ListWrapper.clone(parentVariableNames) : [];
281-
MapWrapper.forEach(variableBindings, (local, v) => { ListWrapper.push(variableNames, local); });
282-
return variableNames;
268+
269+
function _createVariableNames(parentVariableNames, renderProtoView): List<string> {
270+
var res = isBlank(parentVariableNames) ? [] : ListWrapper.clone(parentVariableNames);
271+
MapWrapper.forEach(renderProtoView.variableBindings,
272+
(mappedName, varName) => { res.push(mappedName); });
273+
ListWrapper.forEach(renderProtoView.elementBinders, binder => {
274+
MapWrapper.forEach(binder.variableBindings, (mappedName, varName) => { res.push(mappedName); });
275+
});
276+
return res;
283277
}
284278

285279
function _createElementBinders(protoView, elementBinders, allDirectiveBindings) {

modules/angular2/test/core/compiler/integration_spec.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,24 @@ export function main() {
541541
expect(view.rawView.locals).not.toBe(null);
542542
expect(view.rawView.locals.get('superAlice')).toBeAnInstanceOf(ChildComp);
543543

544+
async.done();
545+
});
546+
}));
547+
548+
it('should allow to use variables in a for loop',
549+
inject([TestBed, AsyncTestCompleter], (tb: TestBed, async) => {
550+
tb.overrideView(MyComp, new viewAnn.View({
551+
template:
552+
'<div><div *ng-for="var i of [1]"><child-cmp-no-template #cmp></child-cmp-no-template>{{i}}-{{cmp.ctxProp}}</div></div>',
553+
directives: [ChildCompNoTemplate, NgFor]
554+
}));
555+
556+
tb.createView(MyComp, {context: ctx})
557+
.then((view) => {
558+
view.detectChanges();
559+
560+
expect(view.rootNodes).toHaveText("1-hello");
561+
544562
async.done();
545563
});
546564
}));
@@ -1343,6 +1361,13 @@ class ChildComp {
13431361
}
13441362
}
13451363

1364+
@Component({selector: 'child-cmp-no-template'})
1365+
@View({directives: [], template: ''})
1366+
@Injectable()
1367+
class ChildCompNoTemplate {
1368+
ctxProp: string = 'hello';
1369+
}
1370+
13461371
@Component({selector: 'child-cmp-svc'})
13471372
@View({template: '{{ctxProp}}'})
13481373
@Injectable()

0 commit comments

Comments
 (0)