Angular 2.x selecting DOM element

后端 未结 5 893
隐瞒了意图╮
隐瞒了意图╮ 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:46

    As of Angular2 Version 2.0.0-beta.17.

    Attribute Directives in Angular2 will do this for you nicely.

    Please see this plunk written in TypeScript. This does what you want nicely.

    The directive file my-highlight.ts has the line:

    this.renderer.setElementClass(this.element, "red", true);
    

    This sets the CSS class on the element.

    While template.html has the actual element which is decorated with the directive [myHighlight]:

    Mouseover to highlight me!

    This, to me, provides the least hack-ish answer to the question without any dependency on jqLite.

提交回复
热议问题