Discovering alterations in input values within reactive forms in Angular 7

Recently, I started working with reactive forms in Angular(v7) for the first time.

I am trying to create a functionality where my submit button becomes enabled when any of the fields are modified. To achieve this, I am utilizing the dirty value of the form. It works well for simple scenarios, such as changing an input type text and enabling the button.

However, I encountered an issue with an element (

<span id="locationUpdated">
) whose value is updated by other JavaScript functions. So, I decided to monitor changes in a hidden input that receives its value in the same way as the span element.

<form [formGroup]="model.form" >
    ....
    <input id="nameInput" type="text"
        [(ngModel)]="model.user.name"
        formControlName="name" />           
    ...
    <span [textContent]="model.user.location.label" id="locationUpdated"></span>    

    <input type="hidden" [value]="model.user.location.label" formControlName="location" />
    ....        
    <button [ngClass]="{'disabled': !model.form.dirty}></button>

</form>

--- Component ---

private buildFormUpdated() {
    this.model.form = this.formBuilder.group({
        name: [this.model.user.name,  [Validators.required]],
        location: [this.model.user.location.label]
    });
}

Initially, I changed the hidden input to text to visualize the value changes, which worked perfectly fine.

However, even after making this change, the dirty property remains false. Only manual changes trigger dirty:true.

Can anyone point out what I might be missing? Any help is appreciated!

Answer №1

What is the key element I am overlooking?

The dirty flag remains false unless there is a manual change made to the data. It only becomes true when the user either moves focus away from the control component or modifies the value through the user interface.

For more information, please refer to the official documentation - https://angular.io/guide/form-validation#why-check-dirty-and-touched

Answer №2

To indicate the control as dirty explicitly, simply add this line of code after executing your logic: this.model.form.markAsDirty();

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

Building interactive web forms with real-time validation using CodeIgniter

I'm looking for a way to use ajax (jquery library) to validate my forms. Specifically, I have a form that requires a minimum password length of 6 characters. Currently, I have implemented the validation rule as follows, $this->form_validation-> ...

What could be causing my data to shift after refreshing in Firefox with the F5 key?

My webpage has four tables, each containing rows with two text boxes filled with numeric values from the server. However, something peculiar occurs. When I add data to a row, let's say row 1, and then refresh the page, two values are mysteriously mov ...

Why does the custom method only trigger once with the addEventListener?

I am attempting to connect the "oninput" event of an input range element to a custom method defined in a corresponding typescript file. Here is the HTML element: <input type="range" id='motivation-grade' value="3" min="1" max="5"> This i ...

"Keep the div locked to the screen when the bottom of the div aligns with the

Hey everyone! I'm currently grappling with a project that requires me to keep a div stuck to the screen (prevent scrolling) when its bottom reaches the bottom of the screen. I have two divs on the page, both with varying heights. My aim is to keep div ...

Looping through an array of images using JavaScript

Currently, I have a computer science test at school, and the main question is to create a traffic light that changes colors in a loop using arrays. I am facing some challenges with this question, but I believe I am close to solving it. Below is my code sni ...

What is the best way to display a fresh jade view when a socket event occurs?

In my project, I am utilizing two key JavaScript files - one on the server side named server.js and another on the client side known as enterchat.js. These files are responsible for communicating via socket.io and all socket events are functioning as inten ...

Can transclusion be achieved while maintaining the directive's scope in Angular?

Can we achieve the following functionality in Angular? <div ng-controller="MainCtrl" ng-init="name='World'"> <test name="Matei">Hello {{name}}!</test> // I expect "Hello Matei" <test name="David">Hello {{name}}!&l ...

Adding custom type definitions for an untyped npm module in TypeScript 2

Despite attempting various methods recommended in other sources, I am struggling to configure a TypeScript project that utilizes an untyped NPM module. Below is a simplified example along with the steps I have taken. Let's imagine, for this demonstra ...

Activate the taskpane window in OfficeJS by simply clicking on the ribbon button

Hello everyone, I am currently working on creating a Word add-in using the Angular framework in OfficeJS. I have managed to add a ribbon tab with a ribbon button, and I would like to toggle the taskpane when the button is clicked. Is there a way to achieve ...

Unable to add an item from an array to the data property in Vue.js

When fetching data from Laravel, I use the following response: $unserialize = unserialize($import->field_names); return response()->json( $unserialize, 200 ) ; On Vue JS, I can view the response using: console.log(response); The data is displayed i ...

Include a link to a JavaScript file within a dynamically created HTML page in a Delphi VCL application

I am currently developing a Delphi XE5 VCL Forms Application which includes a TIdHTTPServer on the main form. Within this server, there is a CommandGet procedure called IdHTTPServer: procedure TForm1.IdHTTPServerCommandGet(AContext: TIdContext; ARequest ...

Customize your JQuery/HTML5 Slider to accept user input

Currently, I am developing a time tracking mobile application in MVC4, which includes the use of a slider input type. <input type="range" name="time_verdi" id="slider" value="7.5" min="0.0" max="24" step="0.5" data-highlight="true"/> When using thi ...

Encountering error TS2305 in Visual Studio Code while working with moment.js in an example from an Angular Material

After referencing the code snippet from https://material.angular.io/components/datepicker/overview#choosing-a-date-implementation-and-date-format-settings, I encountered an issue. While the code successfully compiles and runs, Visual Studio Code flagged an ...

Modifying Fonts on Android Webview in Real Time

Currently, I am facing a challenge with my webview that is loaded with HTML data. My goal is to dynamically change the font of the HTML content during runtime based on user preferences. To achieve this, I have implemented a feature allowing users to select ...

Django form submission triggers Jquery modal to confirm object deletion

I'm currently working with Django and Crispy forms while also utilizing the Shopify Embedded Apps SDK. My goal is to implement a modal window that prompts the user to confirm the deletion of an object. My progress code is attached below. Although I h ...

The callback function in jQuery does not function properly in a custom class or object

Hello, I am new to the world of programming so bear with me if this question seems basic. I am currently working on creating a simple wave effect to give the illusion of moving water. Initially, I achieved this using a straightforward jQuery function which ...

What is the best way to handle a global path parameter in a Nest.js application?

Currently, I am in the process of constructing a rest API for a fully multi-tenant system using a single database and application. To achieve this, I have chosen NestJS as my framework of choice. My goal is to structure all modules based on the :tenantID t ...

What is the significance of labeling a function/method as variadic?

As I was browsing through a blog post discussing Puppeteer (a Node Library designed for browser automation), I came across the following statement: "It is possible to add one or more arguments to page.evaluate because it is variadic in its acceptanc ...

Angular 2 routing allows us to seamlessly display multiple components within the same application

After logging in with the login component, I encountered an issue where both the login and mainpage components were displaying on the same page in Angular 2. In my app.module.ts file: import { NgModule } from '@angular/core'; import { Brow ...

Comparison of URL parameter with property in a filtered array - Next.js/JavaScript

Utilizing Nextjs/Javascript for my project, I am working on filtering the name property within each object of an array based on a comparison function's outcome. However, I have doubts about whether I am approaching this correctly. My current approach ...