Angular 2.x selecting DOM element

后端 未结 5 882
隐瞒了意图╮
隐瞒了意图╮ 2020-12-30 01:06

I know it should be easy but angular 2.0 has no many examples yet..

In one of my components in some case I need to add class on my body tag. But my application is bo

5条回答
  •  春和景丽
    2020-12-30 01:36

    As of angular 2.4 you should inject the DOCUMENT and don't interact with any adapter:

    import { Component, Inject } from '@angular/core';
    import { DOCUMENT } from '@angular/platform-browser';
    
    @Component({})
    export class MyClass {
    
        constructor (@Inject(DOCUMENT) private document) { }
    
        doSomething() {
            this.document.someMethodOfDocument();
        }
    }
    

    Further reading: https://github.com/angular/angular/issues/8509

提交回复
热议问题