Event emitted did not trigger the execution of the associated method

In the following code snippet, I am trying to use an EventEmitter object in the onSubmitPost() method. When I compile the code, the logs in the onSubmitPost method are displayed correctly. However, the logs in the onReceiveSubmittedEvtEmitter method never appear. The HTML code below shows how the EventEmitter is used:

<app-post-create (onPostSubmittedEvtEmitter)="onReceiveSubmittedEvtEmitter($event)"></app-post-create>

The onReceiveSubmittedEvtEmitter method should execute when the event onPostSubmittedEvtEmitter is emitted. Since I do not see any logs from this method, I suspect that the event has not been emitted. Can you please help me understand why the event is not being emitted and how to fix it?

post-create.component.ts:

// Logs in this method are displayed
onSubmitPost(post: Post) {
this.post = {
  title: this.post.title,
  content: this.post.content
};
this.onPostSubmittedEvtEmitter.emit(this.post);
console.log("onSubmit->: post.title: " + post.title);
console.log("onSubmit->: post.content:" + post.content);
}

post-create.component.html:

<form>
<div class="form-group">
  <label>Title</label>
  <input type="text" class="amp-form-control" name="title" required [(ngModel)]="post.title">
</div>
<div class="form-group">
  <label>Content</label>
  <textarea name="content" class="amp-form-control" cols="10" rows="2" required [(ngModel)]="post.content"></textarea>
</div>
<button type="submit" class="btn btn-primary" (click)="onSubmitPost(post)">Create</button>
</form>

app.component.ts:

// Console logs are not displayed
import { Component } from '@angular/core';
import { Post } from './post-create/post-create.component';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent {
  title = 'binding2';
  postsArray: Post[] = [];

  onReceiveSubmittedEvtEmitter(post: Post) {
    this.postsArray.push(post);
    console.log("onReceiveSubmittedEvtEmitter->: post.title: " + post.title);
    console.log("onReceiveSubmittedEvtEmitter->: post.content:" + post.content);
  }
}

app.component.html:

<app-post-create (onPostSubmittedEvtEmitter)="onReceiveSubmittedEvtEmitter($event)"></app-post-create>
<app-post-list [postsToAddToList]="postsArray"></app-post-list>

Answer №1

After some exploration, I stumbled upon the solution while working through the problem. To enhance the functionality of the EventEmitter, add

@Output

like this:

  @Output() onSubmitEventEmitter: EventEmitter<Submission> = new 
  EventEmitter<Submission>();

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 there a way to implement JavaScript within my Blazor server razor page to modify the styling of a blazor bootstrap button after a form submission?

I am currently working on a Blazor server application where I am looking to add a special functionality. My goal is to have the accordion header change to red if validation fails, meaning if there is no input in the field. view image here //index.razor pa ...

Dynamic row insertion in Angular Material table during sorting操作

I utilized Angular material to create a table which looks like this: <mat-table #table [dataSource]="dataSource" matSort> <ng-container *ngFor="let item of displayedColumns; let i = index" cdkColumnDef="{{getColumnName(i)|Formater:'clean&apo ...

What is the best way to locate a particular item in the JSON file for Our World in Data's COVID-19 Master Vaccinations dataset?

As a new coder, I am currently working on accessing specific elements within the COVID Vaccine JSON file from Our World in Data. However, I'm struggling with the syntax of my code and the structure of the JSON file. I have managed to retrieve data fr ...

Rendering only anchor tags in a v-html element while excluding others and displaying them as strings can be achieved by implementing specific filtering logic

Is there a way to display only the anchor tag and render other HTML tags as strings within the v-html attribute of my Vue project? Similar to how LinkedIn does it: example I attempted to use the linkifyjs library, but I couldn't find out how to sole ...

After a duration of 4 minutes, an ERR_EMPTY_RESPONSE is thrown in response to the

My angular 5 node app involves sending a request to the server and waiting for a response. There are instances where the response takes around 5-6 minutes to arrive. However, an ERR_EMPTY_RESPONSE error is triggered by the http method after just 4 minutes ...

How can I make TypeScript's http.get Observable wait for the inline parameter to be returned?

I'm currently facing an issue with my http.get call in which I need one of the parameters (getUserToken) to be returned before the function is executed. However, I don't want to chain them together since the calling function getSomeData returns a ...

What is the best method for retrieving a single resource from the backend using Typescript and createAsyncThunk by obtaining the params ID?

I am currently facing an issue while trying to fetch a single resource (one to-do) from my backend. I am struggling to set up my store in the frontend and cannot seem to find the req.params.id as I would typically do with regular Javascript + Redux thunks. ...

Assistance with Javascript and Jquery for Wordpress

Here's a snippet of my JavaScript code: jQuery(document).ready(function() { jQuery.wiseguys(); }); // To safely use the "$" sign, we are using a plugin structure (function($) { // This is the class constructor or "init" function $.wiseguys = fun ...

Does Mongoose use asynchronous saving?

Just starting out with mongoose and node. I'm trying to determine if the mongoose document.save method is asynchronous. Based on my observations, it seems to work even when not connected. Is there any way to know for sure when the document has been s ...

Tips on adjusting my script to focus on particular text within a div in HTML

I'm currently learning how to code and working on a text generator that allows users to edit text and export it as .DOCX. I came across this script called jsfiddl on the website npmjs, which enables exporting HTML to .DOCX. However, I'm having tr ...

Struggling with organizing nested query data in Firebase/Firestore using React and Typescript

I've been working on rendering data from sections and their corresponding lectures saved within those sections in Firestore. My approach involves querying the section collection first, and then fetching the lecture data using the section ID. While I ...

Unique calculation for rotational movement

I am currently developing a unique compass application. Although the project is progressing well, I am facing a significant challenge with one aspect: My code calculates degree angles within the range of -360 and 360: -318°, -29°, 223°, -163°, ... ...

Fetching a destination through the post approach

Typically, we utilize window.location.href="/index.php?querystring"; in JavaScript. Is it possible to transmit the querystring via a POST method without including any form within the document? ...

First Impressions of Datatables

Is there a way to display a specific range of rows with Datatables initially? For example, showing rows 100-105 only. Does anyone know if this is achievable using the options available? ...

(Angular) Best methods to search for a specific string in an array

Looking to retrieve a string value from an HTML input inside an array in Angular 5 using a service file within a component. My code login.component.ts export class LoginComponent implements OnInit { userData = []; constructor(private router: Route ...

How come my dynamic source path doesn't function correctly unless I add an empty string at the end of it?

Recently, I encountered an issue while using Vue.js to dynamically create a source attribute using an object's properties. Here is the code snippet where I faced the problem: <img :src='"../assets/" + project.image.name + "." + project.image. ...

Encountering a problem with Angular FormGroup losing focus when a key is pressed, preventing the entry of more than one character

Currently, I am attempting to iterate through form controls in order to display fields. While the fields are being shown without any issues and properly binding values upon saving, I have encountered a problem. Whenever I press a key on these fields, the c ...

feed parameter value into a web service using JavaScript

This is my hltm5 code <div data-options="dxView : { name: 'home' } " > <div data-options="dxContent : { targetPlaceholder: 'content' } " > <h1 data-bind="text: message"></h1> <div id="textu ...

How can I move a TableItem from one material UI component to another using drag-and-drop?

I am facing an issue where I need to drag a ListItem from one List component to another. I am uncertain about how to accomplish this in Material UI. ...

Changing a value within a JSON object

I need to update the "Last" Price for MarketName USDT-BTC. How can I change the value from 16750.00000001 to 17000.00000001 and then transmit it through my API? { "success":true, "message":"", "result":[{ "MarketName":"USDT-BTC", "High":16 ...