diff --git a/projects/cdk/schematics/ng-update/v4/steps/templates/migrate-expandable.ts b/projects/cdk/schematics/ng-update/v4/steps/templates/migrate-expandable.ts
index 0d484ee3c68c..be573deb647a 100644
--- a/projects/cdk/schematics/ng-update/v4/steps/templates/migrate-expandable.ts
+++ b/projects/cdk/schematics/ng-update/v4/steps/templates/migrate-expandable.ts
@@ -42,10 +42,14 @@ export function migrateExpandable({
if (expandableValue === 'false') {
const rowsAttr = findAttr(attrs, 'rows');
-
const insertTo = (sourceCodeLocation?.startTag?.endOffset ?? 0) - 1;
+ const insertOffset = templateOffset + insertTo;
+ const selfClosing = template[insertOffset - 1] === '/';
- recorder.insertRight(templateOffset + insertTo, rowsAttr ? '' : '[rows]="1"');
+ recorder.insertRight(
+ selfClosing ? insertOffset - 1 : insertOffset,
+ rowsAttr ? '' : '[rows]="1"',
+ );
}
if (expandableValue !== 'false' && expandableValue !== 'true') {
diff --git a/projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-expandable.spec.ts b/projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-expandable.spec.ts
index fd73d36c5d69..3a88239f445c 100644
--- a/projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-expandable.spec.ts
+++ b/projects/cdk/schematics/ng-update/v4/tests/schematic-migrate-expandable.spec.ts
@@ -37,6 +37,11 @@ const TEMPLATE_BEFORE = `
[formControl]="test"
>
+
+
@@ -70,6 +75,11 @@ const TEMPLATE_AFTER = `
[formControl]="test"
[rows]="1">
+
+