The best way is not to do this.
The browser does layout for a living, driven by the HTML and the CSS. Together, they provide a rich set of capabilities for controlling layout, including media queries, flexbox, and many others.
Accessing an element's height and then adjusting this thing over there or moving this thing over there is a slippery slope. Before you know it, you will end up recreating, and redoing, much of the layout work that the browser is designed to do for you.
If you really want to get the height, there are of course ways to do that, as mentioned in other answers. However, before you start down this road, you should make sure that you really want to, and need to.
If you provide a more specific example of what you intend to do with the height once you retrieve it, people here can probably suggest lots of ways to accomplish the same thing without lots of Angular/TS code.
To accomplish what you want with the textarea replacing the <p>
, you could try this:
let editing = false;
function toggle() {
document.getElementById('div').classList.toggle('editing', editing = !editing);
}
div { border: 1px solid gray; position: relative; }
textarea {
display: none;
height: 100%; width: 100%;
position: absolute; top: 0; left: 0;
}
.editing textarea { display: block; }
.editing p { visibility: hidden; }
<p><button onclick="toggle()">Toggle editing</button></p>
<div id="div">
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</p>
<textarea></textarea>
</div>
As I'm sure you can figure out by looking at the code, the approach is to put both the p
and the textarea
within a div
. The size of the div
will be governed by the size of the p
. The textarea
is positioned absolutely to align exactly with the div
. When in editing mode, the p
is set to visibility: hidden;
, which maintains its position/size in the layout, but hides it, and the textarea
is set to display: block;
so it is shown.
Adapted to Angular, and using the same CSS, this would be:
// component
editable = false;
// template
<p><button (click)="editable = !editable">Toggle editing</button></p>
<div id="div" [class.editable]="editable">
<p>{{myText}}</p>
<textarea [(ngModel)]="myText"></textarea>
</div>