Angular2 retrieve all elements with class name

后端 未结 5 1761
[愿得一人]
[愿得一人] 2020-12-16 09:55

Can anyone help with how to find \'All\' Elements with a particular class name in Angular 2? I thought it would be trivial but it\'s giving me more problems that was prepar

5条回答
  •  余生分开走
    2020-12-16 10:34

    Angular 10+ document compatibility with SSR, import:

    import { DOCUMENT } from '@angular/common'
    

    then inject:

    constructor(@Inject(DOCUMENT) private _document: HTMLDocument) {
        }
    

    And use in your component/ directive like this:

    doSomething(): void {
     this._document.querySelectorAll('.classImLookingFor')
    }
    

提交回复
热议问题