2017-09-21 3 views
-2

したがって、角度2でIDによってオフセットされた要素を取得する方法はありますか?角度2のgetBoundingClientRect()からオブジェクトが 'null'になる可能性があります

このDo'n

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

が、私はこのエラーを取得:助けをObject is possibly 'null'. Thxを。

+1

こちらをご覧ください:https://stackoverflow.com/questions/40349987/how-to-suppress-typescript-error-ts2533-object-is-possibly-null-or-undefine#40350534 – Catalyst

答えて

0

原因このIDの要素がhtmlに存在しない可能性があります。 if文をチェックインしてください。

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

} 
+0

待機を、このidの要素がありますが、動的な要素ではありませんか? – Lukas

+0

これはまったく助けにならない:( – Lukas

+0

は重要ではありませんdocument.getElementById( 'content')はnullを返すことができます – alexKhymenko

関連する問題