Sending information from the parent component to the child Bootstrap Modal in Angular 6

As a newcomer to Angular 6, I am facing challenges with passing data between components.

I am trying to launch a child component bootstrap modal from the parent modal and need to pass a string parameter to the child modal component. Additionally, I want to execute a function as soon as the modal opens, which returns an array displayed in a table.

Parent Component HTML

<div class="parent-comp">
    <div id="ext-modal" class="modal fade" role="dialog">
          <!-- Modal content Starts-->
          <app-f-ext [Id]="Id"></app-f-ext>
          <!-- Modal content Ends-->
        </div>
</div>

Child component TS

import { Component, Input } from '@angular/core';

@Component({
  selector: 'app-premium-extension',
  templateUrl: './premium-extension.component.html',
  styleUrls: ['./premium-extension.component.css']
})
export class ExtensionComponent implements OnInit {
  @Input() Id: string;

  constructor() {

  }

  ngOnInit() {
  }

  callThisFunctionOnOpeningModal(){
   console.log(Id)
  }

}

Child component HTML

<div class="modal-dialog modal-lg">
  <div class="modal-content">
    <div class="modal-header">
      <button type="button" class="close" data-dismiss="modal">&times;</button>
      <h4 class="modal-title">Ext {{Id}}</h4>
    </div>
    <div class="modal-body">

    </div>
  </div>
</div>

Parent Component TS

import { Component, OnInit } from '@angular/core';

@Component({
  selector: 'ap-detail',
  templateUrl: './p-details.component.html',
  styleUrls: ['./p-details.component.css']
})
export class PDetailsComponent implements OnInit {
  Id: string;

  constructor(){
    this.Id = "test";
  }
}

When the modal is opened, the Id is displayed in the header. How can I trigger a function upon loading the modal and pass the Id to that function?

Please refrain from downvoting the question, as I am genuinely struggling with this issue. Thank you in advance for your assistance.

Answer №1

To properly call your child component, it is important to use the correct selector. Therefore, replace:

<!-- Modal content Starts-->
    <app-f-ext [Id]="Id"></app-f-ext>
<!-- Modal content Ends-->

With:

<!-- Modal content Starts-->
    <app-premium-extension [Id]="Id"></app-premium-extension>
<!-- Modal content Ends-->

Don't forget to declare and initialize the Id variable in your parent.component.ts file.

You can then access the Id property within the ngOnInit function like this:

ngOnInit() {
   console.log(Id);
}

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

Is this code in line with commonly accepted norms and standards in Javascript and HTML?

Check out this Javascript Quiz script I created: /* Jane Doe. 2022. */ var Questions = [ { Question: "What is 5+2?", Values: ["7", "9", "10", "6"], Answer: 1 }, { Question: "What is the square root of 16?", Values: ["7", "5", "4", "1"], Answer: ...

Storing Angular header values in local storage

saveStudentDetails(values) { const studentData = {}; studentData['id'] = values.id; studentData['password'] = values.password; this.crudService.loginstudent(studentData).subscribe(result => { // Here should be the val ...

The issue arises when an Angular controller fails to successfully send a value to a web API method using the $http

Here is my AngularJS code snippet: var uname = resetPasswordRequestData.email var request = $http({ method: "post", url: workModule.config.Config.CommonUrl + "api/ResetPasswordRequest", data: uname }); The corresponding Web API code is as fo ...

Exploring the power of $j in JavaScript

Could someone please explain what the $J signifies in this snippet of javascript code? if ($J('.searchView.federatedView.hidden').attr('style') === 'display: block;' || $J('.searchView.federatedView.shown').length ...

Adding dynamic text to a <span> tag within a <p> element is causing a disruption in my layout

I'm encountering an issue with a Dialog box that displays a message to the user regarding file deletion. Here's how it looks: +-----------------------------------------------------------------------+ | Delete File? ...

Utilizing the body in GET requests for enhanced client-server communication

What makes url query strings better than request body values? There are distinct advantages to using url parameters, such as visibility in the address bar and the ability to save requests in the browser history. However, is there more to it? Could reques ...

The world of coding comes alive with Quartz Composer JavaScript

Seeking assistance as I navigate through a challenging project. Any help would be greatly appreciated. Thanks in advance! I have a Quartz Composition where I aim to incorporate a Streetview from Google Maps and control the navigation, but I'm unsure ...

locate the presence of a specific letter within a sequence of characters

When dealing with strings like "Galley1", "Galley2", "Galley3", "Galley4", "Galley5", "Galley6" up to "Galley20", the task is to determine whether a condition is met. The condition should return true if the string contains a number between 1 and 7, inclusi ...

Error: The property value supplied for the calc() function is invalid

<App> includes both <Header> and <Content> components. I am attempting to calculate the height of <Content>, which is equal to the height of <App> minus the height of the header. // App.js import { useStyles } from "./Ap ...

Fill a form with jQuery and ajax data after it has been submitted

I'm working on a simple HTML form and I want to use Ajax to perform a lookup using a PHP file after entering data into the first field. The goal is to fetch information from an external source for the two remaining fields. <form method="post" acti ...

Determine if a webpage forwards to a new link with the help of phantomjs, flagging any issues exclusively for designated websites

As a beginner in frontend development, I have a question that may seem simple. Please be patient with me. var page = require('webpage').create(), system = require('system'); if (system.args.length === 1) { console.log('Usage: ...

Struggling with TypeScript and JsObservable? Let us assist you!

Having previous experience with JSRender, JSViews, and JSObservables, I recently embarked on a new project using TypeScript. Unfortunately, I am struggling to understand how to properly utilize TypeScript in my project, especially when it comes to referenc ...

Adding data to MongoDB using Mongoose and Angular

Recently, I decided to explore nodejs and ventured into using mongoose for mongodb. Below is the code structure that I created, however, I seem to have made a mistake along the way. The scenario is from angular, where I am utilizing $http.post to send an o ...

Is there a way to display sorting icons consistently and in a blue color for the currently selected sorting column in a React material-table component?

I recently used the material-table library to create a material table, but I am having trouble displaying the sorting icon continuously for all columns. In addition, I would like the color of the column that is currently sorted to be blue. If you want to ...

Extracting Node.js data within a Node.js script

Can a node.js file be parsed to execute part of the code within another node.js file? I am attempting to parse a node.js file from within a different script. I do not have control over the contents of the file, but it always contains a function called get ...

Implementing dynamic page loading with ajax on your Wordpress website

I'm currently facing an issue with loading pages in WordPress using ajax. I am trying to implement animated page transitions by putting the page content into a div that I will animate into view. However, my current logic only works correctly about 50% ...

Disabling a specific tab in an array of tabs using Angular and Typescript

Displayed below are 5 tabs that can be clicked by the user. My goal is to disable tabs 2 and 3, meaning that the tab names will still be visible but users will not be able to click on them. I attempted to set the tabs to active: false in the TypeScript fi ...

Tips for creating a console.log wrapper specifically designed for Angular2 using Typescript

Is there a way to create a custom global logging function in Angular 2 TypeScript project that can be used instead of console.log for services and components? I envision the function looking like this: mylogger.ts function mylogger(msg){ console.log ...

Triggering a jQuery click event to showcase the hidden content

I am attempting to replicate the functionality seen on the website. The issue I am facing is related to the event_content class. I want to display only a limited amount of content initially, and then hide the excess content. Upon clicking the plus class, ...

Ag-grid: how to reset a cell when using the "agSelectCellEditor"

According to the documentation on ag-grid: If Backspace or Delete is pressed, the default editor will clear the contents of the cell. However, this behavior does not apply when using the "agSelectCellEditor." Pressing Delete or Backspace puts the cell i ...