Is there any way we can detect if the text is overflow in angular controller?
In my CSS I have the following code:
width: calc(100% - 60px);
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
padding-top: 5px;
And I want to be able to detect if the text is overflow in the angular controller so I can display the tooltip for it. If the text is not overflow then no need to display the tooltip, which was the reason why I want to be able to detect if the text is overflow in the controller.
There is no way for angular (or javascript in general) to know whether an element has used the "...". See this very similar question: HTML text-overflow ellipsis detection.
The best you can do is something like this (where you pass the element you care about in):
function isEllipsisActive(e) {
return (e.offsetWidth < e.scrollWidth);
}
Js courtesy that link.
In addition to Mathew's answer.
I applied that JavaScript logic to an angular 2+ implementation using material components. In this example, you can see that the same JS check is utilized to either disable or enable the Material Tooltip if the text was truncated.
<mat-expansion-panel-header
[matTooltipDisabled]="(titleContent.offsetWidth < matpanelTitle.scrollWidth)"
matTooltip="{{recording.alias}}">
<mat-panel-title #matpanelTitle style="white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: block;">
<span #titleContent>
{{recording.alias}}
</span>
</mat-panel-title>
</mat-expansion-panel-header>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With