Is it possible to spice up functions passed as @Input in Angular with curry?

I have a set of modals with similar styling but completely different functionalities that I need to use in various scenarios within my app. To make it easier for me, I want to pass the logic as input in these different scenarios. When using simple functions (assuming my custom-button requires () => Observable<any> on click), it works smoothly:

export class SimpleComponent {
  @Input() fetchFun: () => Observable<any>;
}

export class ParentComponent {
  funToPass = () => this.httpService.getClients().pipe(...);
}
// in parent component
  <simple-component
    [fetchFun]="funToPass">
  </simple-component>

// in simple component
  <form>
    <custom-button
      type="submit"
      (onClick)="fetchFun"
    ></custom-button>
  </form>

However, when I try to curry those functions with parameters from child components, I encounter the error

core.js:4610 ERROR TypeError: ctx.curriedFun is not a function
. For example:

export class CurryComponent {
  @Input() curriedFun: (dto: Dto) => () => Observable<any>;
}

export class ParentComponent {
  funToPass = (dto:Dto) => () => this.httpService.getClients(dto).pipe(...);
}
// in parent component
  <curry-component
    [curriedFun]="funToPass">
  </curry-component>

// in simple component
  <form>
    <custom-button
      type="submit"
      (onClick)="curriedFun(this.getDto())"
    ></custom-button>
  </form>

Is there a way to successfully pass functions like this to Angular components, or find a workaround for this issue?

Answer №1

Utilize the @output directive in situations like this. Set up an eventemitter to trigger when the button on your modal component is clicked. Handle that event in the parent component and run different functions based on the parent's logic. Consider a new approach - instead of passing functions from parent to child, pass data from child to parent and handle functional logic in the parent component.

Structure:

<parent-component>
 <modal-component></modal-component> 
</parent-component>

//modal component

@Output() valueChange = new EventEmitter<string>();

 buttonClick(value: any) {
   this.valueChange.emit(value);
 }

//modal html

<button (click)="buttonClick(value)">Pass to parent</button>

//parent html

<parent-component (valueChange)='buttonClicked($event)'></parent-component>

//parent component

buttonClicked(event){
 //logic for click action
}

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

Transmitting payment card details to the node

I'm considering utilizing dibs payment dibspayment I came across this Node.js wrapper for the: DIBS API wrapper for Node.js However, using this would involve sending credit card information through a POST request to my node server. My main concern ...

Accessing variables within a JSON string using the Request module in the Firefox Add-on SDK

I am facing an issue with my PHP script on the server. The script retrieves variables from the database and returns a JSON string containing image URLs. Here is a sample of the JSON string: [ { 'src':'imageurl1'} , { 'src':&a ...

Implementing JavaScript from dojo.xhrGet within a Symfony framework

Hey there! I'm diving into the world of dojo and symfony, but I've hit a snag. I'm trying to do an Ajax reload of a section on my page, but it involves executing a JavaScript function. While I've got this down in prototype and jQuery, I ...

What is the best way to search for all results in MongoDB that have x appearing in any property?

Is it possible to search for all pictures in the mongoose framework with node and express that contain a specific parameter, regardless of which property holds that parameter? For example: If I enter "John Snow" in the search bar, I want to see all pictur ...

Deactivating a Component in Ionic Angular

I am interested in including a CanDeactivate Guard in my Ionic Angular project. After reading about the new "CanDeactivateFn" Guard, I have been unable to find any information or examples on how to implement it. Could someone provide me with an example? ...

Display the data in ngx-charts heat map

I have been utilizing ngx charts to display data in a Heat Map. The implementation is smooth without encountering any issues. View Working Example | Heat Map However, I am interested in displaying the values of each grid rather than just on mouse hover. ...

` Why isn't Glide.js functioning correctly when incorporated into a Bootstrap 4 modal component?`

I'm currently utilizing Glide.js and a Bootstrap 4 modal on our team page to showcase the biography of the selected team member. This functionality is achieved by extracting the attribute of the clicked team member and using it as the startAt: index f ...

The issue of Angular 4 routerLinkActive failing to apply the active class has users

I am struggling to style the css and add a class when a link is active using routerLinkActive. Despite success with Bootstrap, when I received custom CSS from the front end developer, the active class fails to be added, even when the route URL matches the ...

Updating an element within an array in a Mongoose database: A step-by-step guide

Currently, I'm looking to make updates to the "like" attribute within an array stored in my mongoose database. { "_id" : ObjectId("59c7eb0c4a992f8702d847a4"), "title" : "Favorite Rapper", "token" : "9b3fd295a1", "votes" : [ { ...

Challenges with JavaScript and JQuery - Extracting and Displaying YouTube Information on Website

While I have some experience with Javascript and JQuery, my latest project on FirstPersonTheater.net is not going as planned. I am using PHP to generate a video ID in javascript code to retrieve information about YouTube videos such as title, uploader, vie ...

Shutting down the server following the completion of the mocha test, yet the data remains intact

Currently, I am working on testing some data using a REST API. The data is stored as an array within the project. My goal is to have the data "reset" every time a test suite runs, so that I can accurately determine the amount of data in the array. Howeve ...

most efficient method to execute numerous API requests at the same time

Currently, I am developing a backend using expressJS. Imagine that I need to make 10,000 calls to an API consecutively and store the obtained data in a database. What would be the most effective approach for achieving this task? Is it possible that Promis ...

What is the best way to utilize getInitialProps specifically during the building process of a NextJS site?

When I am developing a static site using NextJS, I need the getInitialProps method to run specifically during the build process and not when the client loads the page. During the build step, NextJS executes the getInitialProps method before generating the ...

Is there a proper method for populating an HTML text with values from a form?

As a newcomer, I could really use some guidance here :) I'm trying to populate text with various words, numbers, and calculation results from a form. This is my initial attempt for just one field/word, and it appears to be functioning correctly. Do y ...

Printing using *ngFor will display items in an ascending order

When attempting to display an object in markup, I am running into the issue of *ng printing it in ascending order instead of maintaining the original order. Ideally, I would like the elements to be printed as they are. You can view my code on StackBlitz ...

Prevent HTTP using AsyncValidator when the value is empty

I have developed a custom AsyncValidator to verify the uniqueness of a userName. Inspired by this tutorial, I have implemented a delay of 500ms. However, I am facing a challenge in preventing the HTTP service call if the input value does not meet a speci ...

Error: Missing npm install -g @angular/cli@latest package in devDependencies section

ng build is causing an issue that The error reads: Unable to Find npm install -g @angular/cli@latest in devDependencies. When I attempt to start the application using npm start, it works fine. However, while trying to build a file, I encounter this er ...

How do you go about installing the most recent version of a module through Private NPM?

When using a private npm registry, I have noticed that some common commands do not seem to be functioning properly: npm install without specifying a specific @version :: issue npm outdated :: issue npm update :: issue npm view <private-packag ...

Children must be matched to the route before navigating to it

Hello there! I'm having trouble understanding how to navigate new routes with children in Angular 2 rc 4. I'm trying to route to the TestComponent, which has a child, but I keep getting an error message saying "cannot match any route 'test&a ...

Updating input values in Angular 5 with the currency pipe

Currently, I am utilizing the currency pipe to convert input values in Angular 5. However, I have noticed that it only works after unfocusing the input box. I am seeking a solution where the value is masked as soon as it is entered, similar to the example ...