html - 如何使用 ngStyle 根据文本字符定位文本元素?

标签 html css angular typescript

我正在运行 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 元素?

enter image description here

最佳答案

使用 'right':customerLength + 'px'

像这样尝试:

Working Demo

<p id="spandiv" [ngStyle]="{'position':'relative','right':customerLength + 'px','background-color':'red'}">{{customerName}}</p>

关于html - 如何使用 ngStyle 根据文本字符定位文本元素?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59109691/

相关文章:

javascript - Angular.js ng-repeat 迭代远程 JSON 数组问题

javascript - 现在如何知道在 SHAKA PLAYER 中传递内容保护回调?

javascript - Swiper slider 不会为 slider 布局加载多个 View

angular - 嵌入式模板上的任何指令均未使用属性绑定(bind) matHeaderRowDef

javascript - findOneAndUpdate() 基于查找查询条件的子数组

javascript - 我可以在 ng-repeat 中设置变量吗?

html - 将 CSS 与谷歌字体一起使用 - 所有格式?

javascript - 更改 css JQuery 不起作用

javascript - Protractor 等待页面完全加载

javascript - 计算页面上具有 id 属性的列表元素的数量