I'm trying send data from an Angular component to another Angular component.
I've managed to do it through sessionStorage but I understand that it's not the best practise as it should only contain data relative to the user's identification, logging etc.
From several tutorials I've seen, I must pass the data through the html through an EventEmitter. As seen in all those technics Sharing Data between Angular Components - Four Methods
However what I'd ideally like to do is pass data from the parent component to the child component, through the ts components as to be able to manipulate the data directly there and then decide what to do with it in the parent html.
Is there a way of doing this?
Any help would be most apreciated. Thanks!
Here is my Parent component:
import { Component, OnInit, Output } from '@angular/core';
import { CloudinaryModule, CloudinaryConfiguration } from '@cloudinary/angular-5.x';
import { Cloudinary } from 'cloudinary-core';
import { EventEmitter } from '@angular/core';
@Component({
selector: 'app-eacloudinary',
templateUrl: './eacloudinary.component.html',
styleUrls: ['./eacloudinary.component.scss']
})
export class EacloudinaryComponent implements OnInit {
title = "CloudinaryTesting";
private widget:any = null;
// imagesList to stock uploaded Images URL
imagesList: any;
@Output() public found = new EventEmitter<any>(); // emit event for other comp
constructor() { }
ngOnInit(): void {
// init of the imagesList
this.imagesList = [];
console.log("initialized");
(window as any).cloudinary.createMediaLibrary(
{
cloud_name: "cloud_name",
api_key: "0000",
button_class: "myBtn",
username: "username",
button_caption: "Select Image or Video"
},
{
insertHandler: function(data) {
data.assets.forEach(asset => {
console.log("Inserted asset:", JSON.stringify(asset, null, 2));
});
}
},
"#open-btn"
);
this.widget = (window as any).cloudinary.createUploadWidget(
{
cloudName: "cloud_name",
uploadPreset: "testing"
},
(error, result) => {
console.log(error);
console.log("EAcloudinary 44", result);
if (result.event=="success") {
sessionStorage.setItem('imagesURLS', result['info']['url']); // sotcking the url in sessionStorage
let imagesList = [];
imagesList.push(result['info']['url']); // stocking of the URLS into the imagesList
imagesList = [...imagesList];
this.found.emit(imagesList); // here emit event
}
}
);
}
onOpenUpload($event) {
this.widget.open();
console.log("Open upload button is clicked!", $event);
}
}
Here is my Parent html:
<div>
<h1>
Welcome to {{ title }}!
</h1>
<h2>Media Library</h2>
<div id="open-btn"></div>
<h2>Upload Widget</h2>
<div>
<button
id="upload_widget"
(click)="onOpenUpload($event)"
class="cloudinary-button"
>
Upload files
</button>
</div>
</div>
Here is my Child component:
import { Component, OnInit, Input } from '@angular/core';
...
import { Router } from '@angular/router';
import { FormBuilder, FormGroup } from '@angular/forms';
@Component({
selector: 'app-details',
templateUrl: './details.component.html',
styleUrls: ['./details.component.scss']
})
export class UserComponent implements OnInit {
readonly ROOT_URL = 'http://127.0.0.1:8000';
...
@Input() public imagesListURLS : Array <any> = [];
imagesListData = [];
constructor(
...
) {
...
}
ngOnInit(): void {
...
}
...
// bind imagesList
getImagesList(imagesListURLS: any) {
this.imagesListData = imagesListURLS
}
...
}