Join our mailing list for exclusive updates on Angular 6

ingredients : Array
additionalIngredients : Array

In my code, I have two different methods for subscribing:

this.ingredients.valueChanges.subscribe(val=> { 
console.log(val);
}

this.additionalIngredients.valueChanges.subscribe(val2=> { 
console.log(val2);
}

I am looking to combine val and val2 in a single function. How can I achieve this? (or perhaps create val3 which is the sum of val and val2)

Answer №1

When you use combineLatest, you'll get the most up-to-date values of both toppings and toppings2.

Learn more about combineLatest here.

Check out the marble diagram for combineLatest: View the diagram here.

See an example below:

import {combineLatest} from 'rxjs';

combineLatest(this.toppings.valueChanges, this.toppings2.valueChanges )
                          .subscribe( ([topping1val, topping2val]) => {
                                      let topping3 = topping1val + topping2val;
                                 });

Answer №2

zip brings together two observables and waits for both of them to emit. Alternatively, combineLatest can achieve the same outcome regardless of the approach you choose.

zip(this.toppings.valueChanges, this.toppings2.valueChanges).subscribe(val => console.log(val[0] + val[1]))

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

Link the Angular Material Table to a basic array

Currently facing a challenge with the Angular Material table implementation. Struggling to comprehend everything... I am looking to link my AngularApp with a robot that sends me information in a "datas" array. To display my array, I utilized the following ...

What are the reasons behind the inability to implement an Angular custom pipe in a component?

Lately, my focus has been on creating an e-commerce application using Angular 14. One of the key features I am working on is the ability to filter products by brand. To achieve this, I have developed a custom pipe called app/pipes/filter-products.pipe.ts: ...

I am struggling with setting the data received from the backend to an array variable in Angular

Looking for assistance with my interface and service files: [Click here to view the service file code image](https://i.sstatic.net/2f0bzkDM.png) Upon executing the request to retrieve data from the backend and assign it to the variable 'users', ...

How can TypeScript limit the number of properties allowed to be passed as a function parameter?

Here is what I currently have: export interface RegionNode { nodeSelected: boolean; nodeEditable: boolean; zone: Partial<Zone>; parent: RegionNode | null; children: RegionNode[]; } Now, I am looking for a sleek solution to create ...

Angular Universal - Preserving server-side requests for efficient client-side caching

After reading multiple articles on caching data for client in angular universal apps, I am still puzzled about how the data is transferred from server to the client. Should I inject the JSON into pre-rendered HTML or could there be another method that I&ap ...

My component is displaying a warning message that advises to provide a unique "key" prop for each child in a list during rendering

I need help resolving a warning in my react app: Warning: Each child in a list should have a unique "key" prop. Check the render method of `SettingRadioButtonGroup`. See https://reactjs.org/link/warning-keys for more information. at div ...

A guide to submitting forms within Stepper components in Angular 4 Material

Struggling to figure out how to submit form data within the Angular Material stepper? I've been referencing the example on the angular material website here, but haven't found a solution through my own research. <mat-horizontal-stepper [linea ...

Best practices for isolating HTML and XML tags within a PHP array

I am facing an issue with splitting parameters from an array. Here is the code I have tried so far: $test = array ( 'username' => 'vale', 'apiaccesskey' => 'myapi', 'action' => 'p ...

Converting custom JSON data into Highcharts Angular Series data: A simple guide

I am struggling to convert a JSON Data object into Highcharts Series data. Despite my limited knowledge in JS, I have attempted multiple times without success: Json Object: {"Matrix":[{"mdate":2002-02-09,"mvalue":33.0,"m ...

Is it possible to detach keyboard events from mat-chip components?

Is there a way to allow editing of content within a mat-chip component? The process seems simple in HTML: <mat-chip contenteditable="true">Editable content</mat-chip> Check out the StackBlitz demo here While you can edit the content within ...

RXJS - Implementing a conditional logic in the switchMap operator to determine if it's the first

In my service's http fetch method, there is a parameter called "showLoadingSpinner". When this parameter is set to false, the HttpContext DISABLE_LOADER_FOR_REQUEST = true will be set. I would like to set showLoadingSpinner to false for every subsequ ...

Moment.js is stating that there is no property called 'toISOString' on the type '{}'

I'm facing an issue with my code - the `value.toISOString()` function was working fine until now, but suddenly it's throwing a compiler error. I recently upgraded from Angular 7 to 8, which also bumped up the Typescript version to 3.4.5. Any sugg ...

What is the process for including an optional ngModelGroup in Angular forms?

I'm encountering an issue with incorporating the optional ngModelGroup in angular forms. Although I am familiar with how to use ngModelGroup in angular forms, I am struggling to figure out a way to make it optional. I have attempted to pass false, nu ...

Always deemed non-assignable but still recognized as a universal type?

I'm curious about why the never type is allowed as input in generic's extended types. For example: type Pluralize<A extends string> = `${A}s` type Working = Pluralize<'language'> // 'languages' -> Works as e ...

Transferring information through parent-child components via onChange

I have a question regarding data binding. In my project, I have a parent component and two child components: Parent: directives: [firstChild,secondChild], template:' <first-child [showList]="showList" (emitShowList)="getShowList($event)"& ...

Tips on saving text from a file into an array and displaying it at specific positions in Java

How do I extract lines from a text file and store them in an array to print at specific locations? My coi.txt file has 15 lines that I want to display at specified positions through iteration. Can someone please assist me with this task? Thank you for yo ...

Using Angular to convert JSON data to PDF format and send it to the printer

Currently, I am retrieving JSON data from an API and now need to convert this data into a PDF format for printing. I am encountering an issue where the CSS styling for page breaks is not rendering properly within my Angular component. When I test the same ...

How can I transform a javascript object into a fresh array object?

Could you assist me with transforming JSON data? The initial structure is as follows: [ { "_id": "606f990042cc89060c54a632", "number": 1293, "date": "2021-04-08", "__v": 0 }, ...

Angular 2 RC 4's ViewUtils provider offers a range of functionalities for optimizing views

Is there a way to dynamically load a child component in a parent view? this.viewAddedSubscription = viewManager.viewAdded.subscribe((view) => { let injector = ReflectiveInjector.resolveAndCreate([new Provider('view', { useValue: view })] ...

How can I handle a queue in Angular and rxjs by removing elements efficiently?

I'm facing a challenging issue with my code that I need help explaining. The problem lies in the fact that a function is frequently called, which returns an observable, but this function takes some time to complete. The function send() is what gets c ...