Это мой компонент метаданных:Angular2 Renderer setElementStyle не работает
@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]
})
И это моя директива:
import { Directive, ElementRef, OnInit, Renderer, Input } from '@angular/core';
@Directive({
selector: '[myHighlight]'
})
export class MyHighlightDirective implements OnInit {
private _defaultColor : string = "green";
@Input() customColor:string;
constructor(private _elRef:ElementRef, private _renderer:Renderer) { }
ngOnInit():any {
this._renderer.setElementStyle(this._elRef, 'background-color', this.customColor || this._defaultColor);
}
}
Это получаю сообщение об ошибке: «Не удается установить свойство„цвет фона“из не определенно».
Кстати, эта линия работает. 'this._elRef.nativeElement.style.backgroundColor = this.customColor || this._defaultColor; ' –
@ # $ #! Остановить знак подчеркивания для частных объектов! :) https://angular.io/styleguide#03-04 –
Пробовал. Не работает. –