"Capture input value changes and display the previous value when submitting a post. See an example of

Hi there! I'm facing 2 issues with my code, you can find a DEMO here

  1. When adding a product to the sale form, the input field for `description` changes for all products.
  2. Changing the input product in the sale does not reflect the change.

I have shared a demo of the code where these issues occur - please take a look.

I've attempted this solution:

Product TypeScript:

this.myform = this.fb.group({
  'id': new FormControl('', Validators.required),
  'name': new FormControl('', Validators.required),
  'description': new FormControl('', Validators.required),
  'price':new FormControl('', Validators.required),
  'quantity': new FormControl('', Validators.required)

});

Product HTML:

<form [formGroup]="myform" (ngSubmit)="onAddProduct()">
  <h4 style="text-align:center">add product</h4>

   <div class="input-field col s12"> id
      <input formControlName="id" id="id" type="text" class="validate">
   </div>

   <div class="input-field col s12"> name
      <input formControlName="name" id="name" type="text" class="validate">
   </div>

   <div class="input-field col s12"> description
      <input formControlName="description" id="description" type="text" class="validate">
   </div>

   <div class="input-field col s12"> price
      <input formControlName="price" id="price" type="text" class="validate">
   </div>
   <div class="input-field col s12"> quantity
      <input formControlName="quantity" id="quantity" type="text" class="validate">
   </div>
  <br>
  <div id="add_contrat_button_container" class="row">
    <button id="add_contrat_button" type="submit" class="button button1">
      Shto
    </button>
  </div>
</form>

Sale TypeScript:

this.addsale = this.fb.group({
  'invoice_number': new FormControl('', [Validators.required, Validators.nullValidator]),
  'invoice_date': new FormControl('', Validators.required),
  'description': new FormControl('', Validators.required),
  'products': this.fb.array([

  ])
});

Sale HTML:

<form [formGroup]="addsale" (ngSubmit)="onaddsale()" class="col s12">
 <h4 style="text-align:center">add sale</h4>
  <div class="contant">
    <div class="row">
      <div class="input-field col s4">
       invoice_number:
        <input formControlName="invoice_number" id="invoice_number" type="text" class="validate">
      </div>
      <div class="input-field col s4">
        <div class="row">
         invoice_date:
          <input formControlName="invoice_date" id="invoice_date" type="date" >
        </div>
      </div>
      <div class="input-field col s4">
       description:
        <input formControlName="description" id="description" type="text" class="validate">
      </div>
    </div>
  </div>
  <br>
   <table>
    <thead>
      <tr style="color:black;">
        <th>id</th>
        <th>name</th>
        <th>description</th>
        <th>price</th>
        <th>quantity</th>
      </tr>
    </thead>
    <tbody>
      <tr class="group" style="cursor: pointer" *ngFor="let item of products">
      <td>{{item.id}}</td>
      <td>{{item.name}}</td>        
      <td><input formControlName="description" class="validate" [value]="item.description" [ngModel]="item.description" type="text">
      </td>
      <td>{{item.price}}</td> 
      <td>{{item.quantity}}</td> 
        </tr>
    </tbody>
  </table>
  <br>
  <br>
    <button type="submit">
      Register
    </button>
</form>

I would appreciate any help on resolving these issues. Ultimately, I want to achieve:

  1. Ensure that when adding products, the description is unique to each product.
  2. Make sure that changing the product's description in the sale form captures the latest description.

You can view an image here.

Answer №1

Your issue has been successfully resolved with the code provided in this DEMO. The correct value should be set as:

value="{{item.description}}"

In addition to fixing your problem, I have also updated your service to use BehaviorSubject for getting a list of products instead of a single product.

Below is your new Service.ts file:

// Service code goes here

And here is your new HelloComponent.ts code:

// HelloComponent code goes here

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

Is it possible for the binding model of Mat-Checkbox to be optional or null?

Greetings everyone! I am a newcomer to the world of Angular, where I have successfully created a dynamic list of checkboxes. However, I have encountered a challenge when trying to bind selected values from an API to these checkboxes. <div *ngFor="let b ...

Tips for displaying or concealing a div using Angular Js

I have set up two div elements with a dropdown control in one named div1. I am looking to hide div2 until a value is selected in the dropdown. How can I show or hide a div based on ng-change, ensuring that div2 remains hidden until a value is selected? Cod ...

Guide to adding an external script file to an Angular component

Incorporating an external api into my angular project has presented some challenges. In a normal HTML/Javascript setup, all that is required is: <script src="http://api.eventful.com/js/api"></script> <script> EVDB.API.call("/events/get" ...

Exploring [routerLink] vs routerLink: Unraveling the Distinctions

Can you explain the distinction between [routerLink] and routerLink in Angular routing? What are the advantages of each one and which should be used? Understand the difference ...

What is the best way to refine React Component's props with Typescript?

My setup involves utilizing two specific components: Test and Subtest. The main functionality of the Test component is to provide visual enhancements and pass a portion of its props down to the Subtest component. Some props in the Subtest component are des ...

Stop MatDialog from closing automatically when clicked outside while there are unsaved changes

Is there a way to prevent closing when there are pending changes without success? this.dialogRef.beforeClosed().subscribe(() => { this.dialogRef.close(false); //some code logic //... }); The setting disableClose on MatDialog must remain as false ...

Integrate AngularJS service with Angular framework

Attempting to utilize the $log service within an angular 2 app, it seems that the following steps are necessary: Set up a module that includes the service you wish to inject. Utilize UpgradeAdapter's upgradeNg1Provider method. Therefore, I proceede ...

Setting various colors for different plots within a single chart: A step-by-step guide

I'm currently tackling a project that requires me to showcase two different plots on the same chart, one being a "SPLINE" and the other a "COLUMN". My aim is to assign distinct background colors to each of these plots. Please note that I am referring ...

Activate backdrop feature in offcanvas mode

When trying to open an offcanvas panel, I discovered that adding show to its class is necessary for it to open. However, this caused the backdrop feature to stop working, and now clicking outside the panel does not close it. Is there a way to achieve the p ...

Which ngTagsInput version is recommended for Angular instead of AngularJs?

After discovering the ngTagsInput framework on this site, I found it to be a very comprehensive library. However, for Angular 8 users like myself, I came across the ngx-chips framework on this page. While ngx-chips seems to work, I noticed that it lacks s ...

Merge two attributes into a single entity using RxJS

Currently, I am working on handling HTTP responses in the Angular framework and I have a requirement to merge two properties using rxjs. Let's consider a sample response from an API where I need to combine the age with birthday. Since the HTTP respon ...

Customize the date format of the Datepicker in Angular by implementing a personalized pipe

I am dealing with a datepicker that defaults to the MM/dd/yyyy format, and I need it to adjust based on the user's browser language. For example, if the browser language is English India, then the format should be set to dd/MM/yyyy as shown below. Be ...

What is the most effective way to receive all values sent to an Observer upon a new subscription?

I have an observer that receives various values emitted to it at different times. For instance sub = new Subject<any>(); sub.next(1); sub.next(2); sub.next(3); #hack 1 sub.next(4); sub.next(5); sub.next(6); #hack 2 If there is a ...

Utilizing Regular Expressions in Angular 4 by Referencing Patterns Stored in an Object

I'm facing an issue where my code is functional, but I keep encountering a Tslint error that's proving difficult to resolve. This particular configuration worked fine with Angular 1, but now I'm in the process of migrating the application to ...

Creating a carousel with material design aesthetics

I'm working on implementing a carousel in my setup using Angular CLI: 6.0.5, Node: 10.1.0, OS: win32 x64, and Angular: 6.0.3. However, I haven't been able to locate documentation for creating the carousel in Angular's Material Design framewo ...

Adjust the width of Google chart to 100% automatically when the page is resized in Angular version 4 or higher

My application has numerous responsive wrappers on the site, each predefined from an API and containing a Google chart. The problem is that when the page initially loads, the charts are rendered at a specific size, and resizing the window only affects the ...

What is the process for specifying the type for the createApp(App) function in Vue.js 3?

I'm currently working with Vue3 and Firebase using TypeScript. // main.ts import { createApp } from 'vue' import App from './App.vue' import './registerServiceWorker' import router from './router' import store f ...

While attempting to update the package.json file, I encountered an error related to the polyfills in Angular

I have been working on a project with ng2 and webpack, everything was running smoothly until I updated the package.json file. Since then, I have been encountering some errors. Can anyone please assist me in identifying the issue? Thank you for any help! P ...

TypeScript error: Cannot find property 'propertyName' in the 'Function' type

I encountered an issue with the TypeScript compiler when running the following code snippet. Interestingly, the generated JavaScript on https://www.typescriptlang.org/play/ produces the desired output without any errors. The specific error message I recei ...

Split the massive array object into more manageable chunks

I have a large array that can range from 1 to 600K records. To work with this big array, I need to break it into smaller chunks to carry out operations. How can I achieve this effectively? Here is my proposed solution: The challenge I am facing is not kn ...