简体   繁体   English

如何在 Angular 2 - HTML 中绑定另一个元素的宽度?

[英]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"/>

The reference in input tag should be for the <table> 's template variable, not the id. input 标签中的引用应该是<table>的模板变量,而不是 id。 # is not necessary when using it in the expressions. #在表达式中使用时不需要。 You also need to retrieve the offsetWidth and not the width :您还需要检索offsetWidth而不是width

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

In the above code, the table tag width will match input's width.在上面的代码中,表格标签宽度将匹配输入的宽度。

DEMO演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM