Issues with the functionality of Angular 5 EventEmitter

I have been trying to call a function from the Parent Component in the Child Component, and here is how I implemented it:

project-form.component.ts

@Component({
  selector: 'app-project-form',
  templateUrl: './project-form.component.html',
  styleUrls: ['./project-form.component.css'],
  encapsulation: ViewEncapsulation.None
})
export class ProjectFormComponent implements OnInit {

  @Input() project: Project;
  @Output() createProject = new EventEmitter<string>();

  newProject() { 
    console.log("submit");
    this.createProject.emit('submit');
   }
}

project-form.component.html

<button (click)="newProject()" class = "btn-success" >New</button>

project-view.component.ts

export class ProjectViewComponent implements OnInit {

  projects: Project[];
  projectform: Project;

  createProject(event){
    console.log("create Project on parent component");
  }
}

project-view.component.html

<app-project-form [project]="projectform" (createProject)="createProject($event)"></app-project-form>

Upon clicking the New button in the child component, I noticed that only "submit" is displayed in the console, but not

"create Project on parent component"
.

This indicates that the event is not being emitted or received by the parent component. It seems like there might be something missing in my implementation.
I would greatly appreciate any insights or suggestions you can provide.

Answer №1

Hey @artgb, there's one thing you overlooked - createProject(event:string){} Give it a shot.

Answer №2

After testing your code, I can confirm that it works perfectly on Angular 5.0.0. When I click on 'New,' I receive two console messages ('submit' and ' create Project ...'). It appears that the issue you are facing may not be directly tied to the EventEmitter. To successfully compile and execute the provided code snippets, consider making the following adjustments (if necessary):

  1. Include any missing imports.

  2. Implement ngOnInit method.

  3. Remove any references to properties related to the 'Project' model.

Based on my analysis, there seems to be nothing amiss in your current code. The source of your problem likely lies elsewhere.

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

Attempting to create a finalized build of Express using Typescript

Encountering the following error message when executing this command: npm run clear && tsc -P ./tsconfig.app.json && npm run post:build or tsc -p . Node version: v12.13.0 NPM: v6.14.2 Express: 4 Has anyone else faced a similar issue? > ...

Tips on implementing mongoose 'Query<any>' types

I have been exploring ways to incorporate a cache layer into my TypeScript project. Recently, I came across an insightful article on Medium titled How to add a Redis cache layer to Mongoose in Node.js The author demonstrates adding a caching function to m ...

Using VueJs and typescript, transform the input image into Base64 format

Welcome to my first question on this platform! I am looking for a way to convert an input file (image) from a form using VueJs 3 and typescript to Base64 in order to "send" it to my backend (java, spring boot) and store it in mongodb as part of a "User" m ...

Failure to Apply Angular Conditional Class

What is the reason for test-abc not being included? <div class="abc" [class.test-abc]="true"></div> I successfully implemented it with this syntax: [ngClass]="{'foo': true, 'abc': true}" ...

Using Angular to create an Echarts timeline that showcases 3 distinct categories

Here is an example graph created with Highcharts Struggling to replicate a chart like this using Echarts and ngx-echarts. Has anyone encountered this issue before? Currently utilizing the following dependencies: "@angular/animations": "^17 ...

Retrieving JSON data with Angular's HTTP GET method

Can anyone help me with retrieving a json file using a service in Angular 4? Service import { Injectable } from '@angular/core'; import { Http, Response } from '@angular/http'; import { CONFIG as APP_CONSTANTS } from '../config/c ...

Error occurs in the compiler when a variable is utilized as a function

I'm currently facing an issue while compiling Typescript where the compiler is throwing an error: TypeError: myVariable is not a function at Object.<anonymous> (/home/anon/Desktop/Typescript/main.js:37:1) at Module._compile (internal/mo ...

Angular: The fetched data from the API is coming back as undefined

I am trying to use the Highcharts module in Angular to build a chart. The data object needed for the chart is provided by an API, and this is the structure of the object: { "success": true, "activity": [ { &q ...

Issues with displaying data in Angular Material table

I am having trouble displaying data in a table. The data shows up when I display it in another element, but not in the table. Here is my code: <mat-accordion *ngIf="posts.length > 0"> <mat-expansion-panel *ngFor="let post of p ...

Guide on how to update an array within typed angular reactive forms

I'm currently working on finding a solution for patching a form array in a strongly-typed reactive Angular form. I've noticed that patchValue and setValue don't consistently work as expected with FormControl. Here's an example of the fo ...

Angular 2: Simplifying the Process of Retrieving a Full Address Using Latitude and Longitude

Currently, I am utilizing the angular 2-google-maps plugin. Is there a way to retrieve the country and postal code based on latitude and longitude using Angular 2 Google Maps with Typescript? ...

Transforming a function into an array in TypeScript

I attempted to use the map() function on a dataURL array obtained from the usePersonList() hook, but I am struggling to convert my function to an array in order to avoid errors when clicking a button. import Axios from "axios"; import React, { us ...

Tips for effectively narrowing the `undefined` type

Why am I getting this error message? const func = (a: unknown) => { if (a && typeof a === 'object' && 'b' in a) { a.b; } }; The error message I'm receiving is: Property 'b' does not exist on ty ...

Ways to retrieve data from ngrx and display it in a component file

I have recently incorporated the ngrx library into my project to create a more dynamic model for handling data. After setting up my actions, reducers, and effects files, everything seems to be working as intended because I can observe the populated model w ...

"Angular encountered an error while attempting to access the property 'data' from an undefined

I'm encountering an issue when trying to retrieve data from a JSON file. Upon clicking a button to display the data in a textarea, the first click does not yield any result, but subsequent clicks work as expected. The program functions by fetching an ...

Encountering an issue when attempting to integrate material-ui into the jhipster project

After creating a jhipster application which utilizes Angular as the front end UI framework, I am encountering issues with the versions of jhipster and node as specified below: jhipster: 7.9.3, npm: '9.6.5', node: '18.16.0', My objectiv ...

Angular 12 - Directing users to different views depending on their roles

In my situation, the Admin role login will be able to access the Home and UserView Component. After logging in, an Admin will automatically be taken to the Home component. On the other hand, the User role login will only have access to the UserView compone ...

What is the process for defining the type or interface of an object in Visual Studio Code?

Is there a way to create a new type or interface by replicating the structure of a complex object that is imported from a library? For instance, in the image below, the object Text is taken from react-three/drei. https://i.sstatic.net/BcUzd.png Upon inspe ...

An unexpected error occurred in the file node_modules/@firebase/firestore/dist/index.d.ts at line 27:28 - Please insert a ']' at this location

When adding firebase to my Angular app, I encountered an error while running ng serve: ERROR in node_modules/@firebase/firestore/dist/index.d.ts:27:28 - error TS1005: ']' expected. 27 [K in keyof T & string as `${Prefix}.${K}`]+?: T[K]; ...

Tips for resolving the AngularFire migration error related to observables

Recently, I decided to upgrade a project that had been untouched for over a year. Originally built on Angular 10, I made the jump to Angular 12. However, the next step was upgrading AngularFire from v6 to v7, and it appears there is an issue with typings. ...