75
loading...
This website collects cookies to deliver better user experience
text-overflow: ellipsis
feature, since it's unreliable and could break overtime if not monitored during refactoring.slice
pipe where you add the start
and end
of the slice to the interpolated string
<!--Template-->
<a *ngFor="let item of items">
{{ item.name | slice: 0 : 20 }}
</a>
length
<!--Template-->
<a *ngFor="let item of items">
{{ item.name | slice:0:20 }}<span *ngIf="item.name.length >= 20">...</span>
</a>
strings
are but aren't considered strings. Solve this programmatically, by saving a new boolean
value, when the data is either stored or updated:// Component
onSave(event: any, item: Iitem): void {
if (String(this.item.name).length >= 20) {
this.item.maxChar == true;
}
if (event) {
this.dataService.addItem(this.item);
}
}
<!--Template-->
<span *ngIf="item.maxChar == true">...</span>
maxChar
is true; but to remove itself from the DOM when no longer hovering over the item.<!--Template-->
<a
*ngFor="let item of items"
(mouseover)="(item.name.length >= 20) ? onHover($event, item.name) : ''"
(mouseout)="showsTooltip ? (showsTooltip = !showsTooltip) : ''">
{{ item.name | slice:0:20 }}<span *ngIf="item.name.length >= 20">...</span>
<a>
<!--Tooltip-->
<div
*ngIf="showsTooltip"
[style.top]="tooltipTopY"
[style.left]="tooltipLeftX"
class="tooltip"
>
{{ tooltipText }}
</div>
export class SomeComponent implements OnInit {
// Properties
showsTooltip = false; // show or not
tooltipText = ''; // text to be binded
tooltipTopY!: any;
tooltipLeftX!: any;
// Methods
onHover(event: any, itemName: string): void {
this.tooltipText = itemName;
if (event) {
this.showsTooltip = true;
this.tooltipTopY = event.clientY + 'px';
this.tooltipLeftX = event.clientX + 'px';
}
}
}