J'ai un point de terminaison qui renvoie une URL d'image pour moi. Je veux afficher dans un composant et avoir une étiquette sous l'image pour montrer les dimensions de l'image.Tentative d'obtention des dimensions de l'image avec la directive
J'ai écrit une directive d'attribut, GetDimensionsDirective, ci-dessous pour ce faire:
import { Directive, Input, Output, ElementRef, Renderer, EventEmitter } from '@angular/core';
@Directive({
selector: "[get-dimensions]"
})
export class GetDimensionDirective {
@Output() calculatedDimensions = new EventEmitter<any>();
constructor(private el: ElementRef) {
this.getDimensions();
}
getDimensions() {
this.calculatedDimensions.emit({ "height": this.el.nativeElement.offsetHeight, "width": this.el.nativeElement.offsetWidth });
}
}
et je consommeraient donc dans mon élément:
<img get-dimensions (calculatedDimensions)="dimsGot(event)" [src]="image.url" style="width:304px;height:228px;">
et dans mon conponent J'ai la fonction:
import { GetDimensionDirective } from './../../../../shared/directives/get-dimesions';
import { Component, OnInit, Input } from '@angular/core';
@Component({
selector: 'my-image',
templateUrl: './image.component.html',
styleUrls: ['./image.component.css']
})
export class ImageComponent implements OnInit {
image={url: "http://localhost/myApi.WebService/api/resource/12", dimensions:null};
constructor() { }
ngOnInit() {
}
dimsGot(dimensions) {
this.image.dimensions = {};
}
}
Ma méthode "dimsGot" dans mon composant n'est pas entrée, peut-on e conseiller pourquoi?
Vive