I am emitting array of URLimages from child component to parent using EventEmitter.
Child's emitter:
@Output() images = new EventEmitter<string[]>();
Parent's html:
<app-file-upload (images)="onLoadedImages($event)"></app-file-upload>
where <app-file-upload>
is the child.
Parent's OnLoadedImages() function:
onLoadedImages(images: string[]) {
console.log(images);
console.log(images[0]);
}
Why does images[0]
give undefined
when in the console output I can see it has the data and how can I access images: string[]
data?
Edit:
@Colby Hunter As an answer to comment, here's content of child:
@Component({
selector: 'app-file-upload',
templateUrl: './file-upload.component.html',
styleUrls: ['./file-upload.component.css']
})
export class FileUploadComponent implements OnInit {
loadedImagesAsURL = [];
@Output() images = new EventEmitter<string[]>();
constructor() {
}
ngOnInit() {
}
onFileSelected(event) {
const filesList = event.target.files;
for (const file of filesList) {
const reader = new FileReader();
reader.onload = (e: any) => {
this.loadedImagesAsURL.push(e.target.result);
};
reader.readAsDataURL(file);
}
this.images.emit(this.loadedImagesAsURL);
}
}
<div class="text-center" *ngIf="loadedImagesAsURL.length>0" style="height: 300px; overflow: auto;">
<span *ngFor="let image of loadedImagesAsURL">
<img style="width: 100%;" height="400" src="{{image}}">
</span>
</div>
<div>
<input type="file" multiple (change)="onFileSelected($event)" style="display: none;" #fileUpload>
<button type="button" class="btn btn-secondary" (click)="fileUpload.click()">Wybierz zdjęcia</button>
</div>