Issue with child component EventEmitter functionality not functioning as expected

I am working on a CreateProjectComponent, which is a child component. My goal is to close a div in the parent component when the closeModal function is triggered.

Here is the template (html) code:

<div (click)="closeModal(true)"></div>

And here is the component.ts code for CreateProjectComponent:

@Component({
    moduleId: module.id,
    selector: "th-create-project",
    templateUrl: "create-project.component.html",
    styleUrls: ["create-project.scss"],
    directives: [
        ThAutocomplete,
        MD_CARD_DIRECTIVES,
        MD_BUTTON_DIRECTIVES,
    ],
})

export class CreateProjectComponent  {
    @Output() onVoted = new EventEmitter<boolean>();

    public closeModal(agreed: boolean) {
        console.log("vote() on CHILD")

        this.onVoted.emit(true);
    }
}

Now let's look at the parent component:

In the parent component's template, I have the following code:

<div *ngIf="(isNewProjectVisible === true)" (onVoted)="receivedEvent()">

And in the parent component's TypeScript file, I have the following code:

@Component({
    selector: "projects-component",
    templateUrl: `client/+projects/projects.component.html`,
    styleUrls: ["client/+projects/projects.scss"],
    directives: [ProjectListComponent, SelectListComponent, CreateProjectComponent],
    providers: [ProjectService, StateService, CreateProjectService],
})
export class ProjectsComponent extends MeteorComponent implements OnInit {

    public receivedEvent() {  
        this.isNewProjectVisible = false;
        console.log("receivedEvent() on PARENT")

    }

Answer №1

When selecting a component to use, be sure to specify the appropriate selector instead of just using div:

<th-create-project *ngIf="isNewProjectVisible" (onVoted)="receivedEvent()">
</th-create-project>

Keep in mind that simply including div will not automatically create an instance of CreateProjectComponent in Angular when utilizing one of its output properties like (onVoted) in the parent template. Additionally, adding CreateProjectComponent to the directives array will also not instantiate it automatically. To ensure instantiation, explicitly specify it in the template with the correct selector as demonstrated above.

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

Make the switch from TypeScript namespaces to ES2015 modules

After making adjustments to my TypeScript configuration, I am now encountering errors related to the no-namespace rule. Here is how my current setup involving namespaces looks like: Exporting classes within a namespace: namespace MyNamespace { export ...

typescript is throwing an error because it cannot read the property 'filter' of an undefined variable

Whenever this code is executed, the following error is displayed: Cannot read property 'filter' of undefined contrat: Contrat[]; gpss: GPS[]; homeboxp: HomeboxP[]; sensors: Sensors[]; homebox: Homebox[]; getProductName(productid: s ...

Graph your data with a stunning Fusioncharts area range graph combined with a sleek

My goal is to create a visual representation of an area range graph with a corresponding mid line. You can view the image below for reference: https://i.sstatic.net/8KDbF.png While I have successfully incorporated the low and high values, I am encounterin ...

Remove a npm package from an angular application

When I run the following command on the command line and on gitBash: npm -g uninstall angualar2piwik --save I receive the following message: up to date in 0.16s The entry is not removed from package.json, nor are the node_modules removed. I have also ...

Dynamic assignment of values to an object literal in Typescript

I've been troubleshooting this issue for hours and I'm stumped by this specific typescript error. Although I can't share the actual code, I've simplified the problem to the example below: abstract class CustomerType { prote ...

Unable to retrieve iFrame window due to an error

My challenge lies in obtaining the window of an iFrame using this particular code: var frameWindow = document.getElementById("loginframe"); var iWindow = frameWindow.contentWindow; However, I am consistently encountering this error message: Property ...

Why isn't the OrderBy Pipe functioning properly with an array of objects in Angular 4?

I have an array of objects that need to be sorted based on a specific attribute called size. Each object in the array contains attributes {name, size}, and they need to be arranged according to their size. service: search(term: string): Observable<Arr ...

Is there a way to automatically set all object properties to false if one property is set to true in React?

The Issue: My task at work involves creating 3 buttons with separate filters to display different tickets in a table. All the functionality is completed, and the filtered tickets are displayed correctly. However, I am facing an issue that is preventing m ...

No provider for aspnetcore-spa routing

I'm using the Yeoman aspnetcore-spa template with Angular 2. There are essentially 3 key files involved: app.module.client.ts app.module.server.ts app.module.shared.ts I have placed my Service in the providers section of app.module.client.ts and t ...

When trying to open the phonegap-plugin-barcodescanner on Android Studio with Ionic 6 and Capacitor, I encounter an error

Encountered an issue while trying to build the app in Android Studio. The error message "Could not find method compile() for arguments [{name=barcodescanner-release-2.1.5, ext=aar}] on object of type org.gradle.api.internal.artifacts.dsl.dependencies.Defau ...

Only subscribe to Angular 2+ observable when there is a change

Let's look at a situation I'm facing. I have a user service that contains a BehaviorSubject and a method that returns an observable of this BehaviorSubject. Another file is the header component, which subscribes to this observable. The dilemma is ...

Issue: Unable to inject a service into a subscriber in NestJS

Currently, I am working on setting up a subscriber in NestJS to listen for create, update or delete events using TypeORM. When any of these events occur, my goal is to utilize an injected service to generate a new revision entry. However, I have encounter ...

Is it advisable to avoid circular imports in typescript development?

After spending 4 long hours troubleshooting a TypeScript error, I finally found the root cause. Object literal may only specify known properties, and 'details' does not exist in type 'Readonly<{ [x: `details.${string}.value`]: { value: st ...

Issue arises with data types while utilizing the shadcn FormField element

Error: Type 'foo' cannot be assigned to type 'InputProps' Currently, I am attempting to create a reusable component named TextInput that encapsulates the Shadcn FormField component. The documentation specifies the need to pass a "field ...

Bring in the type "Request" from the express-request-id module

Within our web app that utilizes TypeScript and express, we integrated the npm package express-request-id to enhance our HTTP Requests and responses by adding X-Request-Id headers. We crafted a middleware to assign the request's id to the HTTP respon ...

The 'Component' binding element is assumed to have an unspecified 'any' type in TypeScript

Hello, I'm new to coding and encountering an issue with the code below. My goal is to create a protected "Dashboard" page, but I keep getting a binding error that says: Binding element 'Component' implicitly has an 'any' type.ts( ...

Displaying a component inside a different component

I'm attempting to display components inside another component, but even when I try to include div elements within the component, they don't show up. const DisplayComponent = () => { return ( <div> <DisplayContent ...

Ensure that the form is submitted only after confirming it in the modal with react-hook-form

**I am facing an issue with submitting react-hook-form on confirm in a modal component. Whenever the user selects confirm, I need the form to be submitted directly. I have tried writing the modal inside FormSubmitButton. Also, I have tried placing it insi ...

Tips for utilizing the value of object1.property as a property for object2

Within the template of my angular component, I am attempting to accomplish the following: <div> {{object1.some_property.(get value from object2.property and use it here, as it is a property of object1)}} </div> Is there a way to achieve this ...

Having trouble getting matSort to work in Angular 8 as it keeps returning an undefined error when trying

Having trouble getting the mat sort functionality to work on my table, as it keeps showing as undefined. I've tried various solutions from the documentation but nothing seems to be working for me. (I have removed ngIf, changed static to false, and tr ...