Skip to content

How can I bind Width of another element in Angular 2 – HTML?

I have an input and a table.

<input type="search"/>
<table id="myTable"/>

How can I bind the width of my input to the same width of the table?

Something like this?

<input type="search" [style.width]="#myTable.width"/>

Answer

The reference in input tag should be for the <table>‘s template variable, not the id. # is not necessary when using it in the expressions. You also need to retrieve the offsetWidth and not the width:

<input [ngStyle]="{'width.px': myTable.offsetWidth }">
<table #myTable>...</table>

In the above code, the table tag width will match input’s width.

DEMO