TypeScript component has a shared button but the event is not accessible to every page in Angular 4

Within Angular 5, there is a shared button component that is utilized by two distinct pages - page1 and page2. Strangely enough, the click event associated with this button component only seems to be accessible on page1. I am uncertain as to what may be causing this issue. Here is the comprehensive structure of the component hierarchy:

Index.html
   App.html
      page1.html
            btn.html
      page2.html  
            btn.html

It is worth noting that the btn.html component is shared between page1 and page2. The real problem lies in the fact that when the button is clicked, the associated event in btn.component.ts only triggers for page1.html, leaving page2.html clueless about this particular event.

To illustrate my dilemma, I have provided a Plunker code snippet showcasing the issue.

https://plnkr.co/edit/Ry5Uj2VHzVK0RfaJcXyZ?p=info

I would greatly appreciate any assistance or guidance on how to rectify this predicament. Your support is much appreciated. Thank you.

Answer №1

It is possible that the click event is only bound to the first instance of the component. You can attempt the following solution:

In the html file for the btn-shared component:

<div>
  <label>This is a test button</label>
  <input type="button" id="Testbtn" value="Click me" (click)="FunctionOnClick()"> 
</div>

In the ts file for the btn-shared component:

import { Component } from '@angular/core';

@Component({
  selector: 'btn-shared',
  templateUrl: 'src/shared/btn.component.html'
})
export class btnComponent {

public FunctionOnClick(){
  alert("Button Clicked");
}

}

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

Navigational menu routing with AngularJS2 using router link paths

Currently, I am working on developing a navigation menu using angularJS2. Here is the snippet from my app.component.ts: import {provide, Component} from 'angular2/core'; import {APP_BASE_HREF, ROUTER_DIRECTIVES, ROUTER_PROVIDERS, HashLocati ...

The Angular Compiler seems to have a serious issue with the identifier/variable that is being used

Every time I attempt to compile the project, this specific error message appears. Error: ./node_modules/@angular/cdk/__ivy_ngcc__/fesm2015/scrolling.js 23:12 Module parse failed: Identifier 'ɵngcc0' has already been declared (23:12) File was pro ...

The error type currently displayed relates to window['angularComponentReference']

Currently, I am attempting to incorporate NgZone into my Angular project: constructor( private fishboneService: FishboneService, private zone: NgZone, ) { window['angularComponentReference'] = { zone: this.zone, componentFn: (val ...

Manipulating URL parameters in Angular 2

I have implemented the following code: this.router.navigate(['/app/chart', {chartColor: this.color, chartWidth: this.width}]); Upon executing this code, the URL is set to: http://localhost/app/chart;chartColor=blue;chartWidth=600 Everything s ...

Unable to refresh the current active route

After recently updating to the latest versions of RC3 and Router3alpha, I've noticed some changes. One major difference is that clicking on the link of an active route no longer triggers a component reload. How can I achieve this behavior with the up ...

What could be causing the Google Sign-In functionality to fail in an Angular web application?

I'm currently working on implementing Google sign-in for my web app. I've been following this tutorial here. However, I'm facing an issue where the Google sign-in button is not appearing. I would like the authentication to happen at http://l ...

Are items placed into an array passed by reference or by value?

When working with custom Objects in an Angular context, I define two objects "a" and "b" using an interface. These are created as class attributes along with an empty array of these objects. public a: CustomObj; public b: CustomObj; public array: ...

Injecting Parameters into Angular Component Providers

One of my components inherits from another component - @Component({ template: '', providers: [ { provide: NG_VALUE_ACCESSOR, useExisting: forwardRef(() => TComp), multi: true, }, ] }) export abst ...

Avoid fetching components from API in Angular if the data is already present

Within my project, I utilize a component called app-list in all other components. Additionally, there are two separate components named app-cmp1 and app-cmp2. Both app-cmp1 and app-cmp2 include calls to app-list, which retrieves data from an API. By defau ...

Converting a file buffer to upload to Google Cloud Storage

I have been facing an issue while attempting to upload a file to Google Cloud using a buffer and the save function. The problem I am encountering is that even though the files are uploaded to Google Cloud, they are not in the correct format. When I try to ...

How to utilize TypeScript fetch in a TypeScript project running on node with Hardhat?

During a test in my hardhat project on VSCode, I encountered the need to retrieve the metadata object of my NFT from a specified URL. Initially, I assumed I would have to import fs to read the URL. However, while typing out the method, I impulsively opted ...

Utilizing Angular 8's Reactive Form to Transform Checkbox Event Output into a String Format

My form is reactive and includes a field called Status, which can have the values 'A' or 'I': this.form = this.formBuilder.group({ result_info: this.formBuilder.array([ this.getResultcontrols()]), stat ...

AngularJS 2: Updating variable in parent component using Router

My current app.component looks like the following: import { Component, Input } from '@angular/core'; import {AuthTokenService} from './auth-token.service'; @Component({ selector: 'app-root', templateUrl: './app ...

What is the best way to send an email with a time-sensitive code (token) using Firebase?

Currently, I am developing an application that requires sending a verification code to users before they can proceed with certain actions. For instance, when users log in to the app, they need to enter their email, password (authenticated using Firebase au ...

In Typescript, it is not possible to assign the type 'any' to a string, but I am attempting to assign a value that is

I'm new to TypeScript and currently learning about how types function in this language. Additionally, I'm utilizing MaterialUI for this particular project. The issue I'm encountering involves attempting to assign an any value to a variable ...

The namespace does not contain any exported member

Every time I attempt to code this in TypeScript, an error pops up stating The namespace Bar does not have a member named Qux exported. What could possibly be causing this and how can I resolve it? class Foo {} namespace Bar { export const Qux = Foo ...

Creating PDF from HTML in Angular 2: A Step-by-Step Guide

Is there a way to create a download button within my angular2 project that allows users to save HTML div content as a PDF file? Below is the HTML content that needs to be saved as a PDF: <div id="obrz"> <br><br> <p class="float-r ...

defining data types based on specific conditions within an object {typescript}

Can someone help with implementing conditional function typing in an object? let obj = { function myfunc (input: string): number; function myfunc (input: number): string; myfunc: function (input: string|number):string|number { ... } } I've been ...

Whenever I attempt to run the NPM install command in the Terminal, it seems to generate multiple errors

I am encountering an issue on my work laptop. I have the latest versions of Angular, Nodejs, Nodesass, and VScode installed in the local environment path. Whenever I download an Angular template from Github and try to do NPM Install, it consistently thro ...

Tips for preventing repetition in http subscribe blocks across various components

Imagine a scenario where there is a service used for making HTTP request calls. There are two different components (which could be more than two) that need to send the same request using the same observables via this service. After receiving the result, it ...