Is it possible to access NgbdModalContent properties from a different component?

If I have a component with a template containing an Edit button. When the user clicks on it, I want to load another component as a dynamic modal template. The component is named ProfilePictureModalComponent and it includes the Edit button:

(Angular code here)

Here is the template for

profile-picture-modal.template.html
:

(HTML code here)

When the edit button is clicked, this component loads another component called EditProfilePictureComponent within a modal. Here is the code for EditProfilePictureComponent:

(More Angular code here)

The goal is to set the previous profile picture of the user when the Edit button is clicked and the modal template is rendered. To achieve this, I need to call

EditProfilePictureComponent.editProfilePic()
from
ProfilePictureModalComponent.editProfileModal()
. Since there is no direct relationship between the two components, I used a shared service. Now, I also need to access the methods within the shared service.

Answer №1

To transfer the ProfilePictureModalComponent as a parent to EditProfilePictureComponent, you need to call a method of the parent component during the ngAfterViewInit lifecycle hook of the EditProfilePictureComponent. This will allow you to pass the editCropper to the parent ProfilePictureModalComponent:

editProfileModal() {
  const modalRef = this.modalService.open(EditProfilePictureComponent, {size: 'lg'});
  modalRef.componentInstance.user_id = this.user_id;
  modalRef.componentInstance.editProfilePicModal = modalRef;
  modalRef.componentInstance.name = 'World';
  modalRef.componentInstance.parent = this;
}

editProfilePic(editCropper): void {
  console.log("hello");
  let image: any = new Image();
  image.src = "/api/uploadedFiles/profPics/download/"+this.sharedModalService.userProfilePic.name;
  editCropper.setImage(image);
}

Within the EditProfilePictureComponent, make sure to include:

@Input() parent;
......
......
ngAfterViewInit() {
  this.parent.editProfilePic(this.editCropper);
} 

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Passing NextJS props as undefined can lead to unexpected behavior and

Struggling with dynamically passing props to output different photo galleries on various pages. One of the three props works fine, while the others are undefined and trigger a warning about an array with more than one element being passed to a title elemen ...

Finding the precise Time zone with date-fns: A comprehensive guide

I've implemented a date pipe using the date-fns library for formatting dates. Here is the code: date.pipe.ts import { Pipe, PipeTransform } from '@angular/core'; import { format } from 'date-fns'; @Pipe({ name: 'formatDate ...

How can I resolve a route error after converting typescript to es5 in an Angular2 example?

I'm currently diving into the world of Angular2 and exploring how to convert TypeScript to ES5. You can find detailed instructions in this documentation: https://angular.io/docs/ts/latest/tutorial/toh-pt5.html Upon running the ES5 code, I encountered ...

Angular2-starter configuration setup with environment-based variables (such as .env or .conf) for testing and production settings

Frameworks like PHP Laravel often include files for local configuration, separate from dev, test, and production environments. How can a configuration file be provided for an angular-starter project that contains all local environment variable values (su ...

Testing the throwing of errors when running Karma by utilizing sinon.spy on global functions like parseInt

I'm currently facing an issue with monitoring the usage of parseInt within my function. This is a Proof of Concept for integrating TypeScript into our company's workflow. I've tried following two different tutorials on testing Sinon, but no ...

Unable to grasp the mistake

My code contains a function: buy() { return new Promise((resolve, reject) => { this.http.request('http://192.168.1.131:8888/generatetoken.php') .subscribe(res => { resolve(res.text()); }); }).then((key) => ...

Setting up Datatables for Pagination Features

Looking for advice on setting up datatables with paginated results. For example: Here is an example of paginated results from my backend API: { "total": 50, "per_page": 15, "current_page": 1, "last_page": 4, "next_page_url": "http://domain.app ...

What is the best way to execute a function that retrieves data from a MySQL query and then sends it back as a result in Express.js?

How can I refactor my code to efficiently call a function that returns the result of a MySQL query and send it back in an Express.js response? I am attempting to streamline my SQL queries by exporting them into individual functions to eliminate duplicatio ...

Using local fonts with Styled Components and React TypeScript: A beginner's guide

Currently, I'm in the process of building a component library utilizing Reactjs, TypeScript, and Styled-components. I've come across the suggestion to use createGlobalStyle as mentioned in the documentation. However, since I am only working on a ...

What is the best way to call a method from app.component in another component?

Having recently delved into Typescript and Angular 2, I've been struggling to find a solution online that fits my needs. Let's consider the example of an app.component: export class AppComponent implements OnInit { constructor(public _test ...

What is the correct method for packaging and using a TypeScript library built with webpack?

Recently delving into the world of TypeScript, I find myself struggling to grasp how to create a TypeScript library that can be utilized in another TypeScript module through webpack. This particular library is meant to be functional within a browser envir ...

Adding an anchor tag to an ngx-datatable-column can be done by utilizing the properties

My task involves loading data from the server and populating the ngx-datatable. When a specific column is clicked (with either a link <a href="randomurl"/> or [routerLink]="randomcomponent"), it should redirect to a different page or display a modal ...

Exploring the Concept of Template Element Recursion in Angular JS 2

In my Angular 2 project, I encountered a situation where I needed to iterate through ngFor based on child elements. My component should be able to render a list based on the input provided. Here is an example of the data structure: [ { name: 'ABC ...

What are some strategies for accessing the original values of form components that have not been altered when using ngModel?

I am currently developing a form and I intend to utilize the previous values as "value" in the form. By employing ngModel dynamically, I am able to change some properties. However, I have encountered an issue where the field that remains unchanged by the u ...

The playwright signs in to the application through the cached session in global-setup.ts, where the wait for the selector times out when DEBUG=0 but does not time out when DEBUG=

*Updates 22.6.2022 / Identified a recurring issue with OAuth on another site, where globalSetup fails to function properly on the OAuth domain. 21.6.2022 / Analysis from Trace.zip reveals that the OAuth login URL is correct, but the screenshot depicts a bl ...

The directive does not function properly when used across multiple files

Struggling with @Directives and @Hostlisteners - seeking assistance The directive added to the input seems unresponsive, failing to trigger any events or console.log(). I'm puzzled and frustrated as there appears to be a missing piece of the puzzle t ...

Error Type: TypeError when using Mongoose's FindOneAndUpdate function

I am encountering difficulties while trying to implement a findOneAndUpdate query. //UserController UserDAO ['findOneAndUpdate'](userId, {& ...

Applying ngClass to a row in an Angular material table

Is there a way I can utilize the select-option in an Angular select element to alter the css-class of a specific row within an Angular Material table? I have successfully implemented my selection functionality, where I am able to mark a planet as "selecte ...

The Angular service method is producing an error in the production build due to being undefined

When attempting to run my angular app in production mode to ensure everything is functioning properly, I encountered an issue. The app utilizes the swapi API to retrieve actors, which works fine except in the production build where I receive the following ...

The 'ObjectID' property is not present in the 'CollectionStatic' data type

Encountering an issue with the MongoDB npm module: mongoId = new Mongo.Collection.ObjectID()._str; Attached is a screenshot for reference. Appreciate any assistance. ...