How to set attributes on an element in Angular 2?
The 'Angular 2' way would be to use Renderer.
this.renderer.setElementAttribute(e.nativeElement, "spellcheck", "true");
Edit:
As PeterS notes in the comments below, renderer
has been deprecated in favour of renderer2
, so the new command would be:
this.renderer2.setAttribute(e.nativeElement, "spellcheck", "true")
You an use @HostBinding
like
export class Checker {
@HostBinding('attr.spellcheck')
spellcheck:boolean = true;
You can simply declare the host
property in the @Directive
as follows:
@Directive({
selector: "[Checker]",
host: { "spellcheck":"true" }
})
And obviously you can remove the setAttribute()
which you are using in the ngOnInit()
.