Apply the functionality of a method() from component A to component B

Here is the code snippet I am working on:

In this code, a function has been implemented to add two buttons to a component.

Please note that this is purely HTML code.

editFunction = "editRole()";  

getActions(Dropup) {
      return '<div class="dropdown ' + (Dropup ? 'dropup' : '') + '">'
          + '<button class="m-portlet__nav-link btn m-btn m-btn--hover-accent m-btn--icon m-btn--icon-only m-btn--pill" title="Edit"'
          + '(click)='+this.editFunction+'>'
          + ' <i class="flaticon-edit-1"></i>'
          + '</button>'
          + '<button class="m-portlet__nav-link btn m-btn m-btn--hover-danger m-btn--icon m-btn--icon-only m-btn--pill" title="Delete">'
          + ' <i class="fa fa-trash-o"></i>'
          + '</button>';
  }

The issue at hand involves the definition of the edit function in Component A and attempting to call and implement it in Component B.

To address this issue, I have created an array called dataFunctions in Component A

dataFunctions: any[] = [];

In the constructor of Component A, I populate this array with the name of the function

constructor() {
    this.dataFunctions = [this.editFunction];
  }

In Component B, I define a ViewChild:

@ViewChild(CustomDataTableComponent) child;
data : any[];

I then extract the values from the data array:

ngAfterViewInit() {

        this.data = this.child.dataFunctions;
        console.log("output :",this.data);
      }
}

If anyone can offer assistance or guidance on how to resolve this issue, it would be greatly appreciated.

Thank you in advance for your help.

Answer №1

To implement a reference to component B within component A (let's call this reference cmpB), you can use the following code in your HTML: (click)="cmpB.editFonction".

<componentB #cmpB>
</componentB>
<div (click)="cmpB.editFunction()">
    This is where I want the function to be called
</div>

For a demonstration of this concept, you can refer to the example on stackblitz: stackblitz.com/edit/angular-xqpm4x

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

What is the best way to transfer image files into a specific folder?

I am currently in the process of developing a ReactJS web application. My goal is to upload images to a specific folder and then store the file name in the database for future use. Everything seems to be working smoothly up to this point, but I am facing ...

Different approach to tagging div elements

Is there a way to label my divs differently to achieve the layout I need? I have a main div named errors where error messages are displayed. The issue arises when I need to insert form input areas and instruction text in between the error messages and text ...

Steps to deactivate the select element in backbone.js

Can someone help me with disabling the select option in my MVC template code using backbone.js? I tried using readonly="readonly" and disable="disable", but it sends null as value and doesn't update my address. <div class="login-register" data-val ...

The Bootstrap image slider is limited to showing a single item at a time

I've been attempting to implement a carousel in Bootstrap 4 that displays multiple items at once and allows scrolling one item at a time, but I can't seem to get it right. Despite going through various tutorials and forum posts, the carousel alwa ...

Retrieving a video file from the input and showcasing it using Typescript

Currently, I have implemented this code in order to retrieve an image that has been "uploaded" into the browser using the <input type="file"> tag, and then sending the data to a component that will utilize it. fileReady(e) { let file: File = e[ ...

Retrieving a particular value from an array received through Ajax in JavaScript

Ajax functionality $("#panchayat").change(function(){ var param = {'panchayat_id':$(this).val()}; alert (param['panchayat_id']); $.ajax({ type : 'POST', url : '<?php echo base_url();?>index.php/parent_taluk' ...

Managing JSON data through AJAX in ColdFusion

For my external API call using AJAX, I am incorporating a local API setup as an intermediate step. The process is as follows: The Ajax call sends data to localAPI.cfm. Within localAPI.cfm, there is a <cfhttp> tag to forward the data to an external ...

Distinguishing keyboard and mouse events while focusing in React app

I have been working on implementing keyboard navigation focus outline for accessibility. The pseudo class :focus-visible works well on all elements except for input elements like text or textarea. It seems that inputs always have this pseudo class active s ...

Warning: Attempting to activate magellan (or any JS plugin) on an element with an existing Foundation plugin has caused an error in the console

After using bower to install Foundation 6, I keep encountering multiple warning messages in the console whenever I utilize any Foundation 6 - JavaScript based plugin. The specific warning I receive is: Tried to initialize magellan on an element that al ...

The ngModel for a text box does not store the accurate value when the Google Places Autocomplete API is being utilized

I am trying to implement the Google Places Autocomplete API with a textField in my project. The goal is to have city suggestions appear as I type in the textField. Currently, I have bound my textField to a variable called "searchFieldValue" using ngModel. ...

Accessing the name and value of an enum in Typescript

One particular enum is causing some confusion: export enum myEnum { name1 = 'my name', name2 = 'my other name', name3 = 'other' } An object of myEnum has been created: const x = myEnum.name1; console.log(x) // prints ...

Can you identify the mistake in the jQuery function code below?

I have created a jQuery function to retrieve the city and state code based on the zip code entered, but I am encountering some errors. Could someone help me troubleshoot and fix the mistakes in my code? Here is the code snippet: $(document).ready(functio ...

"Implementing a dynamic loading effect in Highcharts triggered by a button

Take a look at this sample highchart fiddle: http://jsfiddle.net/gh/get/jquery/1.7.2/highslide-software/highcharts.com/tree/master/samples/highcharts/members/series-setdata/ $('#button').click(function () { var chart = $('#containe ...

When I implemented snap.js on my main content wrapper, I noticed that it was causing a disruption in a few of my jQuery functions

Currently, I am utilizing snap.js to allow the sliding of the main content div using css3 and javascript, exposing a menu beneath. However, I am encountering an issue when I apply the class snap-content to specify which div snap.js should slide - which is ...

How can you adjust the dimensions of a child div?

Hey there! I've got a div called bat. #bat{ width:50%; height:50%; } Here's the HTML: <div id="bat">dynamic div will appear here</div> When dynamic content is placed inside the div and it's larger than #bat, th ...

Tips for integrating a variety of components onto a single webpage

Exploring the functionality of Angular, I am looking to include multiple components on a single page. How can this be achieved effectively in Angular? I envision each div representing a distinct component and view, with all components residing in separate ...

Even after modifications to the formGroup control, the value remains null when using ng-select, unless the searchTerm is provided programmatically

I need to programmatically set the value of the searchTerm using a virtual keypad, and the dropdown should display options based on the searchTerm. The form control is set to updateOn:'blur'. However, I am facing an issue where the form control ...

What is the reason behind TypeScript prohibiting the assignment of a className property to a context provider?

Embarking on my first journey with TypeScript, I am in the process of reconfiguring a React application (originally built with create-react-app) to use TS. Specifically, I am working with function components and have introduced a global context named Order ...

SEO problem with meta refresh and form field click interruption

I have a webpage that automatically refreshes every 30 seconds with new random content. I am currently using a meta tag to achieve this, but I recently read an article stating that meta refresh is not SEO friendly. Can you suggest a more SEO-friendly metho ...

Verify if the Contact Number and Email provided are legitimate

Is it possible to determine if the phone number or email entered by a user is valid or not? Using Angular 7 and Firebase? ...