Angular2渲染器setElementStyle不工作

Angular2渲染器setElementStyle不工作,angular,Angular,这是我的组件元数据: @Component({ moduleId: module.id, selector: 'si-attribute-directive', template: ` <div myHighlight [customColor]="'red'">Highlight Me..</div> <br> <div myHighlight>Highlight Me Too..</d

这是我的组件元数据:

@Component({
   moduleId: module.id,
   selector: 'si-attribute-directive',
   template: `
      <div myHighlight [customColor]="'red'">Highlight Me..</div>
      <br>
      <div myHighlight>Highlight Me Too..</div>
   `,
   directives: [MyHighlightDirective]
})

它得到的错误是:“无法设置未定义的属性“background color”。

问题是您传递的不是本机元素。试试这个:

this._renderer.setElementStyle(this._elRef.nativeElement, 'background-color', this.customColor || this._defaultColor);

顺便说一下,这条线正在工作<代码>此。_elRef.nativeElement.style.backgroundColor=this.customColor | | this。_defaultColor@#$#!停止对私有属性使用下划线!:)试过了。不起作用。@H.Jabi更新了我的答案。好吧,这行得通。谢谢。
this._renderer.setElementStyle(this._elRef.nativeElement, 'background-color', this.customColor || this._defaultColor);