-2

So, how to get some element offset by ID in Angular 2?

Done this:

scrollToBottom () {
 let content = document.getElementById('content')
 let offsetTop = content.getBoundingClientRect().top
 console.log(content, offsetTop)
}

But I got this error:

Object is possibly 'null'
m02ph3u5
  • 3,022
  • 7
  • 38
  • 51
Lukas
  • 7,384
  • 20
  • 72
  • 127
  • 1
    Take a look here: https://stackoverflow.com/questions/40349987/how-to-suppress-typescript-error-ts2533-object-is-possibly-null-or-undefine#40350534 – Catalyst Sep 21 '17 at 10:24

1 Answers1

0

Cause there can be no element with this id in html. Just check in if statement;

scrollToBottom () {
let content = document.getElementById('content')
if (content) {
   let offsetTop = content.getBoundingClientRect() ? content.getBoundingClientRect().top : 0
   console.log(content, offsetTop)
 }

}
alexKhymenko
  • 5,450
  • 23
  • 40