Is it possible to include multiple eventTypes in a single function call?

I have created a function in my service which looks like this:

public refresh(area: string) {
    this.eventEmitter.emit({ area });
  }

The area parameter is used to update all child components when triggered by a click event in the parent.

// Child Components

this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childFirst') {
        this.loadData();
      }
    });
this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childSecond') {
        this.loadData();
      }
    });
this.myService.eventEmitter.subscribe((data) => {
      if (!this.isLoading && data.area === 'childThird') {
        this.loadData();
      }
    });

// Parent Component

// TS
 showChildFirst() {
    this.navService.sendNavEventUpdate('childFirst');
  }

  showChildSecond() {
    this.navService.sendNavEventUpdate('childSecond');
  }

  showChildThird() {
    this.navService.sendNavEventUpdate('childThird');
  }

 public refresh(area: string) {
    this.myService.refresh(area);
  }

// HTML
<!-- Refresh your childs -->
<button type="button" (click)="refresh()">Refresh</button>

If I call the function with refresh('childFirst'), only the first child component gets updated. Is there a way to modify the function to refresh all child components at once?

Answer №1

If you want to update your 'update' function so that it accepts an array of strings instead of just one string, you can modify it as follows:

 public update(locations: string[]) {
    locations.forEach(location => {
        this.myService.update(location);
    });
}

To call this updated function, simply pass in an array of strings like this:

<button type="button" (click)="update(['location1', 'location2', 'location3'])">Update</button>

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

Divide text to reduce its width within the confines of a specific height on a div element

I've spent the past week scouring various forums, including stackoverflow, but I haven't been able to find a solution. In my responsive website, I'm using CSS flexbox to display dynamic menu items. Sometimes the text can be quite long, and ...

Typescript is missing Zod and tRPC types throughout all projects in the monorepo, leading to the use of 'any'

Recently, I've found myself stuck in a puzzling predicament. For the last couple of weeks, I've been trying to troubleshoot why the types are getting lost within my projects housed in a monorepo. Even though my backend exposes the necessary types ...

Struggling with making react-hook-form correctly validate an email address

I've been struggling for a long time to make this validation work correctly, but it seems impossible. I even added some text at the bottom to display an error message related to the email, but it always shows no error, regardless of the situation. Ed ...

After the completion of the JavaScript timer, the existing text remains in place even when the new text is displayed

https://jsfiddle.net/zLfuwdtu/1/ I've developed a script that tracks down to date 'Date1'. As it counts down, it shows the message "UNTIL FLOW." Once this timer reaches zero, another timer 'Date2' takes its place and displays "ON ...

jQuery triggers change event twice when radio group is manually modified

After selecting "A&L" in the dropdown menu, the radio group is hidden and its value is changed to "n". I attempted to trigger the change event to make the "Hello" message disappear as well, but it seems to be executing twice - once correctly and then ...

Exploring JSON data to locate specific characters with JavaScript

[ {"lastName":"Noyce","gender":"Male","patientID":19389,"firstName":"Scott","age":"53Y,"}, {"lastName":"noyce724","gender":"Male","patientID":24607,"firstName":"rita","age":"0Y,"} ] When comparing my input with the JSON data, I utilize a loop to search f ...

The objects-to-csv module encountered an error: fs.existsSync is not a valid function for this

"TypeError: fs.existsSync is not a function" Whenever I try to create a CSV file and input some data into it, this error message pops up. const objectstocsv = require('objects-to-csv'); export default { data () { return { ...

Is there a way to access a computed property within methods?

Currently, I am utilizing this particular package to implement infinite scrolling in Vue. In order to continuously add new elements during each scroll, I fetch JSON data from my API server and store it in a data object. Subsequently, I divide the array in ...

Is there a way to implement multiple "read more" and "read less" buttons on a single page?

I am currently working on a rather large project and I am encountering some issues with the read more buttons. As someone who is fairly new to JavaScript, I am still grappling with the concepts. While I have managed to make the function work for the first ...

Sloped Divider on the Upper Edge of the Page

I'm currently in the process of developing a new website, and I'm looking to create a unique design for the main navigation bar on the homepage. Here is the ideal layout that I have in mind: https://i.stack.imgur.com/pc8z4.png While I understan ...

Issue with ngStyle not functioning properly with conditional operator

I am currently learning how to use Angular4 ngStyle by going through a tutorial. Here's the code I have been working on: app.component.html <button [ngStyle]="{ 'backgroundColor': canSave ? 'blue': 'gray', ...

Receiving a conduit from the fuel supplier with only limited information on hand

I am designing a service that will utilize pipes as templates. In order to accomplish this, I require access to the registered pipes. The final code structure should resemble the following: @Injectable() class MyService { constructor(private injector ...

What are the steps for creating personalized sliders with WordPress?

Seeking guidance on how to code WP Template files to enable control from the WP dashboard for adding or removing slider images. A sample code snippet is provided below. <div id="myCarousel" class="carousel slide" data-ride="carousel"> <!-- Indi ...

Obtaining a file using capybara independently of rails

Case Study: Attempting to access an external URL using Capybara for downloading a file. It is necessary to use Selenium or Webkit as the driver, since Rack-test does not allow visiting external URLs. This website utilizes iframes. The prompt for file dow ...

refresh Laravel 5.1 webpage content seamlessly without page reloading

Is there a way to update page content in Laravel 5.1 every second without reloading the page for all users? Here is my current view: I'm trying to refresh data without reloading the page using a foreach loop, but I'm not sure how to accomplish ...

Can you explain the significance of this particular method signature in the TypeScript code snippet shown above?

Referencing the ngrx example, we encounter the code snippet for the method store.select, which has a complex signature with two arrows. What is the significance of this method signature? The interface definition in the type file presents the following sig ...

Implement a context path in Angular 2 for enhanced functionality

Is there a way to change the base URL for my app from http://localhost:4200 to http://localhost:4200/pilot/? I attempted to modify the base href in index.html, but encountered an Uncaught SyntaxError: Unexpected token < This is the code snippet from m ...

Is it possible in Typescript to reference type variables within another type variable?

Currently, I am working with two generic types - Client<T> and MockClient<T>. Now, I want to introduce a third generic type called Mocked<C extends Client>. This new type should be a specialized version of MockClient that corresponds to a ...

Is it possible to share an .ics file using SparkPost in a Node.js environment?

Attempting to generate an i-cal event and link it to a sparkpost transmission in the following manner: const event = cal.createEvent({ start: req.body.a.start, end: req.body.a.end, summary: req.body.a.title, description: req.body.a.body, ...