我正在运行 angular,准确地说是 angular 6。我计算了框中显示的给定文本元素的字符数。基于这个值,我想定位标记为红色的元素。我想这样做是因为当字符长度超过 html 元素时,字符的数量不是恒定的。
我尝试过的是通过 ngStyle 应用内联样式。
但这没有用
<p id="spandiv" [ngStyle]="{'position':'relative','right':'var(customerLength)px','background-color':'red'}">{{customerName}}</p>
在 typescript 中,我计算字符并将其分配给变量 customerLength。
let stringValue = customer.AccountID+'('+customer.AccountName+')';
this.customerLength = stringValue.length;
我的问题是,如何使用从 typescript 中获得的值来定位 html 元素?
最佳答案
使用 'right':customerLength + 'px'
像这样尝试:
<p id="spandiv" [ngStyle]="{'position':'relative','right':customerLength + 'px','background-color':'red'}">{{customerName}}</p>
关于html - 如何使用 ngStyle 根据文本字符定位文本元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59109691/