document.getElementById replacement in angular4 / typescript?

后端 未结 5 765
刺人心
刺人心 2020-11-29 03:10

So, im working with angular4 in my practice work and this is new for me. Luckyly, in order to get html elements and its values i used documen

5条回答
  •  猫巷女王i
    2020-11-29 03:40

    You can just inject the DOCUMENT token into the constructor and use the same functions on it

    import { Inject }  from '@angular/core';
    import { DOCUMENT } from '@angular/common'; 
    
    @Component({...})
    export class AppCmp {
       constructor(@Inject(DOCUMENT) document) {
          document.getElementById('el');
       }
    }
    

    Or if the element you want to get is in that component, you can use template references.

提交回复
热议问题