Exploring Angular 2's EventEmitter for Event Handling and Debugging

My attempt at creating a basic event emitter doesn't seem to be functioning properly. Here's the code snippet:

Main Component

This is the main app component I have been working on:

@Component({
    selector:'my-app',
    templateUrl:'app/app.component.html',
    styleUrls:['app/app.component.css'],
    directives:[ROUTER_DIRECTIVES,MenuBar],
})
export class AppComponent{
    title: 'My App'
    
    activated(event: Routes){
        console.log('activated');
        console.log(received)
    }
}

AppComponent.html

<h1>{{title}}</h1>
<menu-bar (activate)="activated($event)"></menu-bar>
<div class="router-wrapper">
    <router-outlet></router-outlet>
</div>

Download

@Component({
    .... // component configurations....
})
class Download implements OnInit {
    ... // some attributes and code ...

    @Output() activate: EventEmitter <Routes> = new EventEmitter<Routes>();
    r: Routes = {};
    
    test(): void {
        console.log('emit start')
        console.log(this.r);
        
        try {
            this.activate.emit(this.r)
        } catch(err) {
            console.log(err)
        }
        
        console.log('sent')
    }
}

The download component is placed inside a router-outlet. Initially, I am able to see all the console log messages from the Download component but not from the app component. Can anyone point out what might be causing this issue?
Additionally, I suspect the problem lies in the event propagation. Is there a way to debug the emitting events globally? If yes, how can it be achieved?

Answer №1

The events triggered by @Outputs() do not propagate and are ineffective in routed components.

To utilize them, you can only employ

<download-component (activate)="..."
(listening directly on the component)

For communication between routed components and parent components, it is recommended to use a shared service as detailed in https://angular.io/docs/ts/latest/cookbook/component-communication.html

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

How can I manually listen to Angular 2 events on a dependency injected instance?

Assume I am working with a component: @Component({selector: 'todo-cmp'}) class TodoCmp { @Input() model; @Output() complete = new EventEmitter(); // TypeScript supports initializing fields onCompletedButton() { this.complete.next(); / ...

Angular: Determining when a form has returned to its original state

My current task involves working with a reactive form that comes with default values. I need to figure out how to prevent the user from saving changes until they have modified something, and then revert back to the initial state. Although I can subscribe ...

Can you explain the meaning of `images:Array<Object> = [];` in TypeScript?

Recently, I stumbled upon this code snippet in TypeScript images:Array<Object> = []; I'm curious, what exactly does the "<>" notation signify? ...

Can we use classlist for adding or removing in Angular 2?

One of the features in my project is a directive that allows drag and drop functionality for elements. While dragging an element, I am applying classes to both the dragged element and the ones it's being dragged over. This is how I currently handle it ...

Tips for updating state in React TypeScript 2.0?

Working with a small component built using React and TypeScript has presented a unique challenge. interface Props { } interface State { isOpen: boolean; } class App extends React.Component<Props, State> { constructor(props: Props) { super ...

What is the proper way to specify the type for a proxy that encapsulates a third-party class?

I have developed a unique approach to enhancing Firestore's Query class by implementing a Proxy wrapper. The role of my proxy is twofold: If a function is called on the proxy, which exists in the Query class, the proxy will direct that function call ...

Two tags attached to four hypertext links

Within my HTML code, I have hyperlinks present on every line. However, I am seeking to eliminate these hyperlinks specifically from "your previous balance" and "your new balance". In the following HTML snippet: <tr *ngFor="let l of statementLines; ...

What is the method for extracting individual elements from an object returned by a React hook in Typescript?

I am currently working on a component that needs access to the query parameters from React Router. To achieve this, I have been using the use-react-router hook package in my project. This is what I am trying to accomplish: import React from "react; impor ...

Samsung browser encounters an international error

After developing my web application using Angular2 Rc1, I noticed that it functions perfectly on Safari, Firefox, and Chrome browsers. However, when trying to access the application on my Galaxy S6 using the default browser, an error pops up: To address ...

Upon attempting to open Google Maps for the second time, an error message pops up indicating that the Google Maps JavaScript API has been included multiple times on this page

Currently, I am utilizing the npm package known as google-maps and integrating it with an angular material modal to display a map. However, upon opening the map for the second time, an error message is triggered: You have included the Google Maps JavaScri ...

Issue with PrimeNG p-editor Appearance

My text editor is not displaying correctly on my website. Please refer to the following images for reference: Top of the page Bottom of the page Currently, it only shows a large SVG and a few input fields at the bottom. The technologies I am using includ ...

What is causing the error even though @angular/core is present in the node_modules folder?

I recently downloaded a sample project from angular.io by following this link: https://angular.io/generated/zips/cli-quickstart/cli-quickstart.zip After downloading, I proceeded to run npm install in the root folder just like in the tutorial found here: ...

Vercel does not support Angular-router

I am currently working on deploying my serverless web MEAN application using Vercel. I have encountered an issue where accessing a route through the navigation menu works fine, but when trying to access the same route directly via http://vercel-dev/some- ...

Dynamically manipulate menu items in material-ui and react by adding, removing, editing, or toggling their state

I have scoured every corner of the internet in search of an answer to this dilemma. Imagine a scenario where there is a menu located at the top right of a navigation bar, initially showcasing TWO options (1. Login. 2. Register). When a user clicks on eithe ...

Importing TypeScript Modules from a Custom Path without Using Relative Paths

If we consider the following directory structure: - functions - functionOne - tsconfig.json - index.ts - package.json - node_modules - layers - layerOne - tsonfig.json - index.ts - index.js (compiled index.ts ...

Receiving a form submission via POST method from an external source without redirection

Situation: A user visits site A and submits a form with a hidden input field containing base64 encoded data. The action leads to site B, which is an Angular 2 application. The structure of the form is as follows: <form id="partner" method="POST" actio ...

Displaying a dynamic array in Angular that shows all results, not just the data under a

Trying to grasp the complexities of Angular 5, I am faced with a challenge. The code successfully passes the "id" number from the array to the URL, but when accessing model/1, all objects from the array are displayed instead of just the object under id 1. ...

Reusing Ionic headers across multiple pages

I'm looking to reuse my HTML header across different pages within an Ionic 4 app. Here is a snippet of my code: header.html <ion-view> <ion-grid> <ion-row> <ion-col col-6 class="font"> < ...

Tips for delivering a variable to a React Native Stylesheet

Is there a way to pass a variable to the "shadowColor" property in my stylesheet from an array declared in the code above? I keep encountering a "Can't find name" error. Attempting to use a template literal has not resolved the issue. Any assistance w ...

The use of async/await within an observable function

I am looking to establish an observable that can send values to my observers. The issue lies in the fact that these values are acquired through a method that returns a promise. Is it possible to use await within the observable for the promise-returning f ...