Shutdown the modal directly from the main component

Currently, I am working on a parent component called List which has a property addModal: boolean;. Within the template of this List component, there is a button that shows a modal component when clicked. Here is the code snippet:

<button 
    (click)="addModal = true">
    Show Modal
</button>

The modal component (app-modal) is a separate entity from the parent component. My goal is to be able to close the modal from within the app-modal component using an EventEmitter. However, I am facing some challenges with implementing this functionality. This is what I have so far in the Modal component:

@Output() addModal = new EventEmitter();

decline() {
   this.addModal.emit(true);  //?  console logs the EventEmitter object
}

In the Modal template:

<button (click)="decline()"></button>

I understand that I should be listening for this event in the parent component but I am struggling with the implementation. Any assistance would be greatly appreciated.

Answer №1

To monitor emitted events on the main element where $event represents the output value, use true as shown below:

<app-modal *ngIf="addModal" (addModal)="doSomething($event)"></app-modal>

In terms of event naming, it's advisable to choose a descriptive name like declined instead of addModal.

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

"Encountering issues with logging in through AngularJS and the $http request functionality

My goal is to login using $http and GET of REST web services. This is my approach: Retrieve data based on username and password using $http.get Store the result in $scope.getResult=res; Assign variables to the retrieved data: var result=$scope.getResult ...

There seems to be a problem passing props as I am unable to read the property 'map' due to a TypeError

Trying to pass props from one component to another component in a different file. I have set up the constructors correctly in both components, but still encountering an error that I'm struggling to resolve as a newcomer. I've searched on Google a ...

Efficiently handling multiple JSON objects in a single PHP function

Currently, I am working on a project that involves populating two dropdown menus where the value of one depends on the other. Specifically, I have three dropdowns - one to select a class, and the other two for selecting subjects and exams based on the sele ...

Dealing with a POST array object in JSON format while preventing the occurrence of the ECONNREFUSED error that results

I have a data.json file containing an array of objects with around 4000 entries, each object having approximately 200 properties. However, when I try to read this file and POST it to a loopback API, I encounter an error message stating "ECONNREFUSED Socket ...

Disabling the visibility of elements through a transparent sticky-top menu

I'm having an issue with my website design. I have a gradient background and a sticky-top menu on the site. The problem is that when I scroll down, the content appears through the menu, which is not ideal. I don't want to apply the same gradient ...

Navigate to a different page in AngularJS using ngRoute without utilizing ng-view

I have a web application dashboard with a common header across all HTML files, including the profile page, page 1, and SignIn. However, I want the SignIn page to be different without the common header. How can I redirect to signin.html without using ng-vie ...

"Subscribing in interceptor doesn't seem to have any effect

I am currently facing an issue where I try to refresh a token using an API call, but the token does not get refreshed and the logs are not appearing as expected. In my code, I have implemented a timeout for testing purposes to trigger the token refresh and ...

What is the process for interacting with pseudo HTML elements with Selenium WebDriver?

Is there a way to interact with pseudo HTML elements using Selenium WebDriver? For instance, elements like input::after and input::before that don't appear directly in the DOM but are visible on the page. ...

What is the best way to extract values from a specific table column and store them in an array using Angular?

I have a section of code containing a table in my component: expect-next-month.component.html <table id="users"> <tr> <th>Number of month</th> <th>Total checking e ...

Is it possible for jQuery to navigate to a different page, extract around 10 links, and then incorporate them into the original page?

Apologies if the title of my question is unclear, allow me to clarify. I'm interested in exploring the possibility of a specific task and would appreciate guidance on how to proceed. While I haven't attempted it myself yet, I am eager to learn mo ...

Elucidate a crucial aspect within a broad context

It seemed like I had a good grasp on how to tackle this, but clearly there's a misstep somewhere. I'm aiming to create a function that acts as a typeguard; its main purpose is to ascertain whether an input is an object containing a specified key ...

Tips for preventing a bootstrap 5 button group from automatically centering after being clicked

I am facing an issue with a Bootstrap 5 button group inside a modal. The height is set to style="overflow-y: auto; max-height: 290px; min-height: 290px;" Whenever a button inside the modal div is clicked, the scroll centers to the middle button. I want to ...

Are you incorporating multiple renderers into your Three.js project?

Is there a way to utilize two WebGL renderers (using Three.js) simultaneously within the browser? I attempted this by connecting each renderer object to individual 'div' elements: <div id="ThreeJS1"...> <div id="ThreeJS2"...> ...

Why is the script from URL "http://.../js/myscript.js" not executing as expected after being fetched?

Is it possible to have the server run a JavaScript file specified in a URL, such as "http://.../js/script_name.js", and return the result instead of just displaying the source code? I am assuming that the server is using node.js. ...

Set up a local storage system to store the background color for my page

I have a unique feature on my website where clicking a button generates a random background color each time. However, I am looking to enhance this by implementing a localstorage function that will remember the last clicked color even after closing or reloa ...

Utilize a promise or await statement with a dynamically generated string variable

Currently, I am constructing a mongoose query and saving it in a variable named query. Below is the snippet of code showcasing this: let query = "Product.find(match)"; if (requestObject.query.sortBy) { query = query.concat(".", &quo ...

Encountering the 'data is null' error when making a Twitter API request, yet the data is successfully displayed in the browser

I am attempting to show the number of followers for a Twitter account, but when I connect to the API using this code: $.getJSON("https://api.twitter.com/1/users/show.json?screen_name=uswitchTech&include_entities=true", function(data) { console.log ...

Tips for sending the image file path to a React component

Hey, I'm working on a component that has the following structure: import React from "react"; interface CInterface { name: string; word: string; path: string; } export function C({ name, word, path }: CInterface) { return ( < ...

Trying to understand the purpose of this Angular function has been quite puzzling for me

In my angular/node sample code, I identified a function that performs a specific task. However, I am puzzled as to why it is named differently from what I expected. By adding a console.log() statement in the code snippets, I was able to confirm that the f ...

Sending the appropriate context using "this" to an external function within a class

Imagine a scenario where I have a simple class that extends other classes, and an object of functions that I am passing to class B const actions = { doSomething: (this: B, foo: boolean) => { console.log('from do something', this.text, ...