Which event is triggered following the update of all values in reactive forms?

How can I properly emit an event in Angular (v.5 or v.6) after a specific input of a reactive form has been changed?

Here are the approaches I have tried:

  1. (ngModelChange)="doSomething($event)"
    (HTML, basic event)

  2. (bsValueChange)="doSomething($event)"
    (HTML, ngx-bootstrap specific event)

  3. Basic solution for reactive form with one input - subscribe to event (TS) like:

this.reactiveForm.get('inputName').valueChanges.subscribe(
  value => { this.doSomething(value); }
);

However, none of these methods work in all scenarios.

Testing:

  1. Set input value A

  2. Change input value to B

Expected behavior:

  • All items in reactiveForm.value should have their current values when calling doSomething().

Results:

Approach 1: Unable to replicate this issue in demo. It works on my demo, but not in my larger project. When setting date A and then selecting date B, the "doSomething" function logs date A as the actual field value. The demo works fine, but the behavior is inconsistent in the project.

Approach 2: Does not work at all. This is a specific event for ngx-bootstrap, but even after selecting date B, it still logs date A.

Approach 3: Also does not work. This is a pure reactive form solution. You can test it on my demo with the last input. Typing A followed by B results in only logging A as the value of reactiveForm.value['item']. Despite subscribing to the valueChanges event of the reactive form input, the value remains unchanged.

My question is:
What can I use to capture an event for a single field of a reactive form after the values in the reactive form have been updated?

Here is the StackBlitz code DEMO with example

PS: Initially, I suspected it was a problem with the ngx-bootstrap extension, but the issue also persists with pure reactive form inputs.

Answer №1

According to Atiris, the answer is 3.

However, it's important to note that:

this.reactiveForm.value['testDateE'] //or this.reactiveForm.value.testDateE

differs from

this.reactiveForm.get('testDateE').value;

The reason for this difference is that when checking in HTML, the second method must be used. When submitting a form, we send reactiveForm.value with no issues. Similarly, if you write {{reactiveForm.value |json}}, you'll see the current values.

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

Utilizing Firebase Cloud Functions to perform querying operations on a collection

Imagine having two main collections: one for users and another for stories. Whenever a user document is updated (specifically the values username or photoUrl), you want to mirror those changes on corresponding documents in the story collection. A sample u ...

When using Typescript with MUI styled components, there may be issues with recognizing common objects for styles

I'm facing a challenge where I have various styled components with some shared styles. To address this, I decided to create a function that takes in a `theme` parameter and outputs the common styles being used. Here's a glimpse of what I came up ...

Discover the latest Angular edition through coding

Is there a simple way to display my Angular version on a website using code instead of checking it in the command line with 'ng --version'? ...

How can we enhance intellisense to recognize instance members of an interface when using dynamic indices?

In the midst of developing an angular project, I am currently utilizing an interface to specify a configuration for a module. The design of the interface revolves around mapping names to objects and is relatively straightforward: export interface NamedRou ...

Tips for injecting Angular service for login in Cypress tests

Recently, I decided to incorporate Cypress into my testing process for my Angular application. Following Cypress's recommendation, I aimed to streamline testing by skipping the login screen and directly accessing my Angular LoginService. To guide me ...

Extracting information from Timeless-time-picker utilizing form control

<timeless-time-picker [hourFormat]="'hours24'" [visibleItemsCount]="3" [size]="'medium'" [theme]="'dark'" [selectionHighlightStyle]="'none'" [selectionBoxBackgro ...

Collaborate on Typescript Interfaces within a Firebase development environment

I've been developing a Firebase project using Angular for the frontend, and incorporating the @angular/fire library. Within this project, I have created multiple interfaces that utilize firebase and firestore types. For example: export interface ...

Sharing the input value with a service in Angular 4

I am a beginner when it comes to Angular 4. I currently have a variable named "md_id" which is connected to the view in the following way. HTML: <tr *ngFor="let item of driverData"> <td class="align-ri ...

Managing the appearance of one DOM element using another DOM element

Hey there, I'm currently working on an Angular Material tooltip implementation. When I hover over my span element, the tooltip appears. Now, I'm wondering how I can dynamically change the background color of the tooltip based on certain condition ...

How to customize Material UI Autocomplete options background color

Is there a way to change the background color of the dropdown options in my Material UI Autocomplete component? I've checked out some resources, but they only explain how to use the renderOption prop to modify the option text itself, resulting in a a ...

Angular is showing an error stating that the type 'EventEmitter' is not generic

As I follow along with a tutorial, I've come across the use of EventEmitter. The code snippet provided in the tutorial is as follows: @Output() ratingClicked: EventEmitter<string> = new EventEmitter<string>(); However, my Visual ...

Unveiling the seamless integration of TypeScript with webpack/metro mainFiles module resolution

Scenario Setup In Webpack, the mainFiles module resolution feature allows for resolving specific files based on the environment. This concept is exemplified by: | Button | | - index.ts | | - index.performer.ts | | - index.customer.ts // page.ts im ...

Display spinner exclusively for prolonged requests using Angular's HttpInterceptor

I developed a custom interceptor that displays a spinner whenever an HTTP request is initiated: import { HttpEvent, HttpHandler, HttpInterceptor, HttpRequest } from '@angular/common/http'; import { Injectable } from '@angular/core'; im ...

Next.js is refusing to render an array of HTML elements

Consider this scenario where I have a block of code in TypeScript that attempts to create and display a list of elements. Here is a sample implementation: const MenuList = ():ReactElement => { const router = useRouter(), liElements:any = []; con ...

Allowing the use of a string as a parameter in a Typescript constructor

Currently, I am utilizing TypeScript to create a constructor for a model within Angular. One of the attributes in the model is configured as an enum with specific string values. Everything functions well if an enum value is passed to the constructor. The i ...

In React, how do public services compare to the Angular equivalent?

In order to maintain the state of a service between different views, I utilize Angular services that are declared as public. This allows me to store the necessary information within the service's variables. For example, in View 1, I am able to scan a ...

Is it possible to extract Apollo type guards from a package?

I came across a helpful Apollo type guard that I want to integrate into my app. Here is the code snippet: export function isField(selection) { return selection.kind === 'Field'; } You can find it in node_modules/@apollo/client/utilities/grap ...

Node-server hosted in Azure experiencing difficulties with websockets connectivity

I have a simple example that works fine on my local machine but fails to work properly when deployed on Azure. The original issue I had related to a CORS error, which I have since resolved by editing it out. However, I am still struggling to get WebSockets ...

Issue with executing a server-side function in a Next.js application

I'm encountering an issue with my Next app. I have a method in my ArticleService class that retrieves all articles from my SQL database. async getArticles(): Promise<IArticle[] | ServiceError> { try { const reqArticles = await sql< ...

Tips for preventing duplicate data fetching in Next.js version 13

I am currently in need of retrieving information from the database, generating metadata, and displaying the page content. The current method I am using is as follows: export const generateMetadata = async ({ params: { questionSlug }, }: Props): Promise&l ...