import { Directive, HostBinding} from '@angular/core';
@Directive({
selector: '[myDirective]',
})
export class MyDirective {
@HostBinding('class')
elementClass = 'custom-theme';
constructor() {
}
}
You can now easily use the directive:
<div myDirective> </div>
If you prefer to use it directly inside a component:
import {Component, Renderer2} from '@angular/core';
@Component({
selector: 'app-test',
templateUrl: './test.component.html',
styleUrls: ['./test.component.scss']
})
export class TestComponent {
constructor(private renderer: Renderer) {}
click(event) {
renderer.addClass(event.target, 'popup');
}
}