Angular 2 event emitter falling behind schedule

I am currently utilizing Angular 2 beta 6.

The custom event I created is not being captured

import {Component, OnInit, EventEmitter} from 'angular2/core';
import {NgForm}    from 'angular2/common';
import {Output} from "angular2/core";
@Component({
    template: '<form class="form-horizontal" (ngSubmit)="onSubmit()" #registrationForm="ngForm" style="color: #676767">
         <button type="submit" >Send Event</button>
   </form>'

})
export class registrationComponent {

    @Output() logged: EventEmitter<any> = new EventEmitter();

    onSubmit () {
        console.log(Button clicked);
        this.logged.emit(null); // Emits an event on clicking the button
         }

}

In addition to that, I have added another component which listens for my event 'logged'.

import {Component} from 'angular2/core';

@Component({
    selector: 'message-com',
    template: '<div (logged)="getLoggedIn($event)">Hello<div>'
})
export class MessageComponent {


    getLoggedIn() {

        alert("Event received ");

    }
}

Although I see "Button clicked" in my console, I am actually anticipating "Event received "

Answer №1

templateUrl: <div (registered)="getUserRegistered($event)">Welcome<div>'

must now be changed to

template: '<div (registered)="getUserRegistered($event)">Welcome<div>'

also note that a <div> does not trigger the registered event.

Answer №2

It appears that there may be an issue with connecting your two components together (in addition to the problem pointed out by @Günter). The registrationComponent is not being utilized in the MessageComponent, preventing you from capturing the event as intended.

To address this, consider refactoring your components in the following manner:

  • Include a selector for the registrationComponent:

    import {Component, OnInit, EventEmitter} from 'angular2/core';
    import {NgForm}    from 'angular2/common';
    import {Output} from "angular2/core";
    @Component({
      selector: 'registration',
      templateUrl: 'views/registration.html'
    })
    export class registrationComponent {
      @Output() logged: EventEmitter<any> = new EventEmitter();
    
      onSubmit () {
        this.logged.emit(null);
      }
    }
    
  • Add the registrationComponent to the directives attribute of the MessageComponent and utilize it using its selector:

    import {Component} from 'angular2/core';
    import {registrationComponent} from '...';
    
    @Component({
      selector: 'message-com',
      template: `
        <registration (logged)="getLoggedIn($event)">Hello</registration>
      `,
      directives: [ registrationComponent ]
    })
    export class MessageComponent {
      getLoggedIn() {
        alert("Event received");
      }
    }
    

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

The map failed to display any information

<div> {!props.isLoading && <div> {normalizedData.map((outerObj,index) => { { <p className="space_name"> {outerObj.space_name} </p> ...

A pair of buttons each displaying a unique div block

I am facing an issue with my jQuery script. I want to be able to click on one of the previewed text associated with a button and then have the other one close automatically. The desired effect is for the text to slide down using slideDown() and disappear ...

React Router Blank Page Conundrum

In a new project, I'm experiencing an issue where the content is not loading in despite using a similar React-Route setup that worked in a previous project. When I create a nav link in the root directory, it changes the path but the screen remains whi ...

Creating a default option of "please select" on an Angular select element with a null value for validation

Within my Angular application, I am using a select element with an ngFor loop: <select formControlName="type" required> <option *ngFor="let type of typeList" [ngValue]="type.value">{{ type.caption }}</option> </select> When view ...

Issue encountered: `property does not exist on type` despite its existence in reality

Encountering an issue in the build process on Vercel with product being passed into CartItem.tsx, despite being declared in types.tsx. The error message reads: Type error: Property 'imageUrl' does not exist on type 'Product'. 43 | ...

Ways to retrieve parameters in getStaticPaths function?

I'm currently working on a Next.js app with Contentful as the CMS. The file structure relevant to my question is: pages -[category] -[slug].js My goal is to access the category value when a user visits category/slug. Currently, I have the category ...

ngx-datatable detail row failing to expand properly

I am striving to develop an ngx-datatable component that can be utilized universally for consistent styling and functionality. Although most features are working correctly, I'm struggling to understand why the details row isn't expanding as expe ...

Validating Components that Adhere to an Interface

When working with Angular, there is a need to specify the type for a component that implements a particular interface and is passed into a class. For example, consider Class A with the following signature: class A { constructor(public component: ?) {} } ...

Get all Facebook friends that can be tagged using Angular

Seeking to access all taggable Facebook friends of a user. As Facebook only shows 25 friends at once, I aim to include a button for loading the next set of users. (I understand that I could increase the limit, but sticking to 25 per request is more effici ...

The statusText variable for getXMLHTTP object is not found when the status is not equal to

Earlier, I posted about this issue before isolating the problem. Now that I have isolated it, I wanted to repost with a clearer focus on the two functions causing the problem. Whenever I update my State, it triggers the getCity function. The call is being ...

Achieve a Smooth Transition: Utilizing Bootstrap 3 to Create an Alert Box that Fades In upon Click and Fades Out

Incorporating AngularJS and Bootstrap 3 into my web app, there is an "Update" button that saves user changes. Upon clicking the "Update" button, I aim to trigger and display bootstrap's alert box with the message "Information has been saved," followed ...

Show angular variable data in a table based on their index

I have an array of angular variables with values like [ 170, 1, 130, 3, 134, 4.... and so on]. I would like to present these values in a table format where the even positioned values are shown in one column and the odd positioned values in another column. ...

Two separate menus for each root file in Ionic 2

In my Ionic 2 project, I have developed two root files: app.component and report-menu. In order to make the report-menu module accessible in the app.component.module.ts file, I imported it successfully. However, I am facing an issue where one menu is dis ...

Background PHP/JS authentication through HTTP

Recently, I developed a PHP website that includes embedded web-cam snapshots which refresh every 2 seconds using JavaScript. For the first camera, I can easily log in using URL parameters like this: cam1-url?usr=usr&pwd=pwd. However, the second camer ...

Issue with JQuery: Inability to deactivate an element after receiving an Ajax response

My dynamic dialogue box, generated via Ajax return, presents a challenge involving the dynamically changing drop-down list element $('#functionSelect'). I require this list to trigger disabling of input fields within the dialogue box upon changes ...

What happens in mongoose if one of several consecutive queries fails?

Currently, as I work on my API, I am utilizing two Models: SongModel and UserModel. Whenever a new song is saved, I also execute a query to link the song._id to the user who created it. Unfortunately, a mistake in my code caused the second query to throw a ...

What is the best way to incorporate AngularJS data into JavaScript for use in Google Chart?

How can I leverage my AngularJS scope data in a Google Chart or JavaScript? Below is my AngularJS file: angular.module('reports').controller('ReportInfoCtrl', ['$scope', 'reports', '$rootScope','$loca ...

Enhance vue.js by adding your own custom filters

I am currently working with Vue.js integrated with Laravel using Elixir and Browserify. I am trying to create custom global filters, each in their own separate file. Despite following the documentation, I am encountering an issue where I receive the follow ...

Encountering a configuration error in the Nginx.conf file while attempting to use

I recently obtained a Visual Studio solution that includes multiple projects. https://i.sstatic.net/HuRyl.jpg A Nginx.conf file was created in ClientApp/Angular. https://i.sstatic.net/CN65e.jpg This is my docker-compose file: clientapp: image: ${DOCKER ...

Bug in auto compilation in Typescript within the Visual Studios 2015

Currently, I am utilizing Visual Studio Pro 2015 with auto compile enabled on save feature. The issue arises in the compiled js file when an error occurs within the typescript __extends function. Specifically, it states 'Cannot read property prototyp ...