Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -471,17 +471,18 @@ export function getMainBlock(etm: ElementToMigrate, tmpl: string, offset: number

// the beginning of the updated string in the main block, for example: <div some="attributes">
let start = tmpl.slice(etm.start(offset), attrStart) + tmpl.slice(valEnd, childStart);
// the middle is the actual contents of the element
const middle = tmpl.slice(childStart, childEnd);
// the end is the closing part of the element, example: </div>
let end = tmpl.slice(childEnd, etm.end(offset));

if (etm.shouldRemoveElseAttr()) {
// this removes a bound ngIfElse attribute that's no longer needed
// this could be on the start or end
start = start.replace(etm.getElseAttrStr(), '');
end = end.replace(etm.getElseAttrStr(), '');
}

// the middle is the actual contents of the element
const middle = tmpl.slice(childStart, childEnd);
// the end is the closing part of the element, example: </div>
const end = tmpl.slice(childEnd, etm.end(offset));

return {start, middle, end};
}

Expand Down
39 changes: 39 additions & 0 deletions 39 packages/core/schematics/test/control_flow_migration_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -611,6 +611,45 @@ describe('control flow migration', () => {
].join('\n'));
});

it('should migrate a bound NgIfElse case with ng-templates and remove all unnecessary attributes',
async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
import {NgIf} from '@angular/common';

@Component({
templateUrl: './comp.html'
})
class Comp {
show = false;
}
`);

writeFile('/comp.html', [
`<ng-template`,
` [ngIf]="fooTemplate"`,
` [ngIfElse]="barTemplate"`,
` [ngTemplateOutlet]="fooTemplate"`,
`></ng-template>`,
`<ng-template #fooTemplate>Foo</ng-template>`,
`<ng-template #barTemplate>Bar</ng-template>`,
].join('\n'));

await runMigration();
const content = tree.readContent('/comp.html');

expect(content).toBe([
`@if (fooTemplate) {`,
` <ng-template`,
` [ngTemplateOutlet]="fooTemplate"`,
` ></ng-template>`,
`} @else {`,
` Bar`,
`}`,
`<ng-template #fooTemplate>Foo</ng-template>\n`,
].join('\n'));
});

it('should migrate a bound NgIfThenElse case with ng-templates with i18n', async () => {
writeFile('/comp.ts', `
import {Component} from '@angular/core';
Expand Down
Morty Proxy This is a proxified and sanitized view of the page, visit original site.