5

Is there a way in Angular2/Typescript to detect if the div is overflow with text depending on the width of the screen. If the text is overflow, I want to display a "More" button. There's also a bookmark icon on the right side, so I have to take that into account of the div width.

In my HTML:

<div *ngFor="let i of items"> {{ i }} 
  <i class="fa fa-bookmark" aria-hidden="true"></i>
</div>

//If the text is overflow
<button *ngIf="overflow"> More </button>

In my typescript:

@HostListener('window', ['$event'])
public checkOverflow() {
    console.log(event.target.innerWidth)
}

The question is how to find out what's the div width considering there are other elements on the side. Is there another way to check if the string is overflow in javascript/typescript side?

Matt-pow
  • 946
  • 4
  • 18
  • 31
  • Possible duplicate of [Angular 2 Read More Directive](http://stackoverflow.com/questions/37819312/angular-2-read-more-directive) – David Chelliah May 17 '17 at 06:20

1 Answers1

26

HTML File

<div #theElementYouWantToCheck>
  .
  .
  .
  <button *ngIf="checkOverflow(theElementYouWantToCheck)"> More </button>

</div>

TS File

  checkOverflow (element) {
    return element.offsetHeight < element.scrollHeight ||
           element.offsetWidth < element.scrollWidth;
  }
Community
  • 1
  • 1
Onurhan Aytac
  • 424
  • 3
  • 6