currently using angular 5 cli. i don't know why my custom directive not working. and not even showing error in my console. am trying to some style to maintain full image width.
so far.
import { Directive,ElementRef,Renderer } from '@angular/core';
@Directive({
selector: '[fullWidthImg]'
})
export class MyStyleDirective {
constructor(private el: ElementRef, private renderer:Renderer){
renderer.setElementStyle(el.nativeElement,'background-image', 'url("https://mdbootstrap.com/img/Photos/Horizontal/Nature/full page/img(20).jpg")')
renderer.setElementStyle(el.nativeElement,'height', '100%")')
renderer.setElementStyle(el.nativeElement,'background-position', 'center')
renderer.setElementStyle(el.nativeElement,'background-repeat', 'no-repeat')
renderer.setElementStyle(el.nativeElement,' background-size','cover')
}
}
copied from other resource
import { Directive, ElementRef, AfterViewInit } from '@angular/core';
@Directive({
selector: '[fullWidthImg]'
})
export class MyStyleDirective implements AfterViewInit {
constructor(private elRef: ElementRef) {
}
ngAfterViewInit(): void {
this.elRef.nativeElement.style.backgroundImage = 'url("https://mdbootstrap.com/img/Photos/Horizontal/Nature/full page/img(20).jpg")';
//this.elRef.nativeElement.style.fontSize = '20px';
}
}
<div fullWidthImg></div>
where am doing wrong. if possible pls provide live example or good resource to understand custom directive
thank you
Try this One:
<div fullWidthImg [ngStyle]="{'height': '200px', 'width':'200px'}"> </div>
Rest of code is working just change div tag only!! Here is working example : Image display