I am trying to change the background color of a div using a custom directive, but its not working, below is the code of my component generated using angular cli.
import {
Component,
Directive,
Renderer,
ElementRef,
NgModule,
OnInit } from '@angular/core';
@Directive({
selector:"[ccCardHover]"
})
class CardHOverDirective {
constructor(private el: ElementRef,
private renderer: Renderer) {
renderer.setElementStyle(el.nativeElement, 'backgroundColor', 'blue');
}
}
@Component({
selector: 'app-custom-directive',
template: `
<div class="panel panel-default" ccCardHover>
<p class="panel-body">Body text</p>
</div>`
})
export class CustomDirectiveComponent implements OnInit {
ngOnInit() {}
}
Any ideas why is not making the background of "panel panel-default" blue?
After a more careful review of my code, I noticed two things
Export the custom directive:
export class CardHoverDirective { ..
Import and declare it in the app.module.ts
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With