The values for the FormControl number are predetermined, but they will only appear once they have been interacted

Currently, I am facing an issue where the form input fields are not being populated with data from the model that needs to be edited. While the text input fields function correctly, the number fields remain empty until I click on them, and in some cases, they only update after clicking on all previous number fields. Here is the HTML code snippet:

<form [formGroup]="servis.editCarFrom" style="width: 320px;">
    <div class="form">
    
        [...]
        
    </div>
</form>

In addition, here is the section of code responsible for setting the control values:

InitEditCarForm(kola: any) {
    let naziv = kola.naziv.split('-');
    let marka = naziv[0];
    let model = naziv[1];
    let tip = TipVozila[kola.tipVozila];
    
    [...]

    this.editCarForm.markAllAsTouched();
}

Despite this implementation, I am noticing a delay in updating the values on the page, and not all fields are getting updated as expected. Is there a more efficient approach to handling this? If not, how can I ensure that the number fields display their correct values automatically once the page has finished loading?

Answer №1

When declaring your form group, you have the option to set initial values for each form control.

formGroup = new FormGroup({
   firstInput: new FormControl(value1),
   secondInput: new FormControl(value2)
});

To update the values in your form group later on, you can use either setValue or patchValue directly on the form group. The difference is that setValue requires updating all inputs, while patchValue allows updating specific controls without affecting others. Here's an example:

formGroup = new FormGroup({
control1: new FormControl('initial value'),
control2: new FormControl(null),
control3: new FormControl(12) 
});

function updateAllValues() {
  this.formGroup.setValue({
    control1: 'new value',
    control2: true,
    control3: 42
  );
}

function updateSomeValues() {
  this.formGroup.patchValue({
    control1: 'new extra value',
    control3: 42
  );
}

Answer №2

I encountered a similar issue that was resolved by implementing an *ngIf directive on the element containing all the lists/methods that may not have been initialized at the beginning.

For instance, it seems like your "GetTipovi()" method could be returning an incomplete list when the page initially loads. However, due to two-way binding, your loop still populates with values whenever "GetTipovi()" eventually returns something.

Consider adding an *ngIf directive to the element displaying the list returned from "GetTipovi()" to potentially address this problem.

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

Incorporate HTML elements within an Angular component

Looking to dynamically add an Angular HTML element to a component at runtime. Imagine the HTML element is an H1 tag with an Angular expression: <h1>{{testHeading}}</h1> The goal is to insert this tag into a component while preserving its dy ...

Issue with vendor.bundle.js while Using Angular2 App on Internet Explorer 11

My Angular 2 application runs perfectly on Google Chrome. However, when I try to open the same application in Internet Explorer 11, nothing shows up. An error is displayed in the console: https://i.sstatic.net/2cDhw.png The issue seems to be in the vend ...

Issue encountered while attempting to access a GitHub repository

I am currently utilizing Angular 2 and attempting to directly reference a GitHub repository instead of an npm package for debugging purposes. However, the project fails to compile. In my packages.json file, I made the change from "primeng": "4.2.2", to "pr ...

Learn the correct way to export a React class component with proper type declarations

Encountering an issue when exporting a React class component for use in Enzyme, as it is exporting as a function type: class MyComponent extends React.Component<MyComponentProps, MyComponentState> { ... } export default connect(null, mapDispatchToP ...

Passing along the mouse event to the containing canvas element that utilizes chart.js

Recently, I created a custom tooltip for my chart.js chart by implementing a div that moves above the chart. While it works well, I noticed that the tooltip is capturing mouse events rather than propagating them to the parent element (the chart) for updati ...

Is there a way to refresh a JSON file in an Angular app without needing to recompile the entire application?

I am currently extracting information from a JSON file stored in my assets folder and displaying it in a table format. At the moment, the JSON file is only loaded during the Angular application build process. I want to make it possible to dynamically load ...

Using createStyles in TypeScript to align content with justifyContent

Within my toolbar, I have two icons positioned on the left end. At the moment, I am applying this specific styling approach: const useStyles = makeStyles((theme: Theme) => createStyles({ root: { display: 'flex', }, appBar: ...

Angular: Converting this code into a loop - A step-by-step guide

Currently, I am in the process of learning Angular along with its Angular Material UI Framework. Following the installation of dependencies, I created a Material Rank Table using the command below: ng generate @angular/material:table rank-table --module=ap ...

What is the best way to determine the total of values from user-input fields that are created dynamically

Scenario- A scenario where a parent component is able to create and delete input fields (child components) within an app by clicking buttons. The value of each input field is captured using v-model. Issue- The problem arises when a new input field is crea ...

Running multiple chained, variable dependent pipes in Angular 2: A step-by-step guide

I am working on a task where I need to organize the array users based on the User's selection, and then divide the result into pages of 20 for easy navigation. The template I am using contains a table structured like this: <table> <thead ...

What is the method for uploading a file from a form using Client.post() in a Django test?

Currently, I am developing a Django test that involves uploading an .xml file. Whenever the file is correct (validated by the schema), its data will be added to the project's database. Below is a snippet of the code from the view: if request.method ...

The Angular Material Table is refusing to display external data when the page first loads

I have been working on a project using Angular 6, where I am fetching data from an AWS DynamoDB table and displaying it in a Material Table component through Observable. Initially, I created the table structure with angular-cli and then integrated my own s ...

What is the access URL when using Angular 2's npm start command?

When I execute npm start in my angular 2 project directory, the console response includes the following lines: Access URLs: ------------------------------------ Local: http://localhost:3000 External: http://10.28.93.96:3000 ------------------------- ...

How can one effectively showcase boxes? (Algorithm, UI)

Currently, I am working on creating a React Typescript View to display daily Appointments, similar to an outlook calendar. However, I'm encountering an issue when the Appointments overlap. How can I determine their width and position in such cases? It ...

Having trouble manipulating text or values of angular elements with Selenium and Python

https://i.stack.imgur.com/vZdo0.png I am facing an issue with a date input field that does not have a calendar or dropdown for selection. I tried using driver.find_element_by_id('dataInicio').send_keys(date_value) but it doesn't seem to work ...

The TypeScript factory class anticipates an intersection

In my code, I have a class factory called pickSomething that generates a specific type based on a key provided from a ClassMap: class A { keya = "a" as const; } class B { keyb = "b" as const; } type ClassMap = { a: A b: B } c ...

Create a Typescript index signature that incorporates individual generic types for each field

Many times, the keys of a record determine its value. For instance: const record = { [2]: 5, ["string"]: "otherString", ["there is"]: "a pattern" } In these instances, each key of type K corresponds to the ...

Ensure that in Django bootstrap4, the parameter "form" is populated with a functioning Django Form

I encountered a Bootstrap exception that says: 'Parameter "form" should contain a valid Django Form.' This occurred while trying to execute the code provided in this tutorial: {% load bootstrap4 %} <form action="/url/to/submit/" method="po ...

Tips for determining if an array of objects, into which I am adding objects, contains a particular key value present in a different array of objects

I've been working on this and here is what I have tried so far: groceryList = []; ngOnInit() { this._recipesSub = this.recipesService.recipes.subscribe((receivedData) => { this.loadedRecipes = receivedData.recipes; }); } onCheckRecipe(e) { ...

Tips on instructing TypeScript to view a parameter as a namespace instead of a class, especially when they share the same name - gRPC

Apologies for the lengthy title... I am in the process of developing a basic crud system using gRPC and typescript. My issue lies in the fact that the auto-generated file creates a class and a type for each parameter in my protoFile. For example, the User ...