Sending updated form entries using ngForm

I have been struggling to submit a form with default values already set in the form field. The HTML code I am using is as follows:

<form #updateEndpointForm="ngForm" (ngSubmit)="updateEndpoint(updateEndpointForm);">
            <section class="form-block">
                <div class="form-group">
                    <input type="text" placeholder="id" name="id" [value]="updateEndpointData?.id" [hidden]="true" ngModel required>
                </div>
                <div class="form-group">
                    <label for="endPointType">Endpoint Type</label>
                    <input type="text" placeholder="Endpoint Type" name="endPointType" [value]="updateEndpointData?.endPointType" ngModel required readonly>
                </div>
                <div class="form-group">
                    <label for="name">Endpoint Name</label>
                    <input type="text" placeholder="Endpoint Name" name="name" [value]="updateEndpointData?.name" ngModel required>
                </div>
            </section>
            <button type="submit" class="btn btn-primary">ADD</button>
        </form>

Although the data in the corresponding field displays on the HTML page, when I submit the form, the

console.log(updateEndpointForm.value);
output shows it as empty
{"id":"","name":"","endPointType":""}
. Only the modified field's value is displayed. My goal is to gather all values from updateEndpointForm, not just the ones that are modified. How can I achieve this without using two-way binding [(ngModel)]? Edit: I prefer not to utilize two-way binding.

Answer №1

Presented below is the effective solution utilizing Two-way data binding:

HTML File,

<form #updateEndpointForm="ngForm" (ngSubmit)="updateEndpoint(updateEndpointForm);">
  <section class="form-block">
      <div class="form-group">
          <label for="endPointType">Id</label>
          <input type="text" placeholder="id" name="id"  [hidden]="true" [(ngModel)]="updateEndpointData.id" required>
      </div>
      <div class="form-group">
          <label for="endPointType">Endpoint Type</label>
          <input type="text" placeholder="Endpoint Type" name="endPointType" [(ngModel)]="updateEndpointData.endPointType" required readonly>
      </div>
      <div class="form-group">
          <label for="name">Endpoint Name</label>
          <input type="text" placeholder="Endpoint Name" name="name" [(ngModel)]="updateEndpointData.name" required>
      </div>
  </section>
  <button type="submit" class="btn btn-primary">ADD</button>
</form>

Typescript File,

//A sample variable 'updateEndpointData' has been declared with initial data
public updateEndpointData:any={"id":1,"name":"Muthu","endPointType":"test"};

updateEndpoint(data){
    console.log("form data",data.value);
}

Output Snapshot, Please note that the initial value of "Muthu" was dynamically changed to "Muthukumar" during execution.

https://i.sstatic.net/8DqET.png

For a live working example, refer to StackBlitz 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

Create a variable that is assigned the value of a route parameter, but leave it undefined if the parameter cannot be

I am working on an Angular 7 component where I extract a route parameter value: export class PostComponent implements OnInit { postId: number; constructor(private route: ActivatedRoute) { } ngOnInit() { console.log(this.postId); this.ro ...

Angular - Enhancing the page with valuable information

Recently, I've been developing an Angular application that is designed to function as a digital magazine. This app will feature articles, news, reviews, and more. Along with this functionality, I am looking to include an admin panel where I can easily ...

``If you're looking to integrate a 360-degree product viewer into your Angular application, here is a

I am in need of showcasing a 360 Product viewer consisting of an array of 36 car images in base64 format. Despite attempting to utilize the angular-three-sixty Package by @mediaman, I was only met with an empty canvas. Does anyone have experience in implem ...

angular determine if a column exists within a matCellDef iteration

Is there a way to check a column in an ng for loop in Angular without using the logic "{{element[p.key] != null ? '$' : ''}}" for a specific column or excluding a specific column? View image description here #html code <table mat-t ...

You must provide a secret or key in order to use the JwtStrategy

I have encountered the following error and I am unsure of its cause. Can you assist me? ERROR [ExceptionHandler] JwtStrategy requires a secret or key TypeError: JwtStrategy requires a secret or key at new JwtStrategy (C:\Users\wapg2\OneDriv ...

Does angular-sortablejs work with angular 5?

I attempted to use angular-sortables in combination with the ng-drag-drop library to sort the list items that are being dragged, but it appears that nothing is happening when I try to use it. Does anyone know if angular-sortables is compatible with Angular ...

What is the best way to include text or a label on my Angular map without using a marker?

I am currently using the agm-map module in my angular project. I want to place a label or text at the center of a polygon on the map without using markers. How can I achieve this functionality in Typescript? I attempted to use the MapLabel Utility for thi ...

Creating a custom dropdown filter in ag-grid-angular

I am currently integrating 'ag-grid-angular' into my project. https://i.sstatic.net/9jcy2.png As for the filter display, I would like to replace the search box with a drop-down menu. Furthermore, I aim to populate this drop-down with unique ele ...

Using Typescript for AngularJS bindings with ng.IComponentController

Currently, I am utilizing webpack alongside Babel and Typescript Presently, the controller in question is as follows: // HelloWorldController.ts class HelloWorldController implements ng.IComponentController { constructor(private $scope: ng.IScope) { } ...

Utilizing Angular's asynchronous validators to handle incoming response data

Struggling with async validators in Angular and trying to implement Container/Presentational Components architecture. Created an async validator to check for the existence of an article number, with the service returning detailed information about the arti ...

Losing the generic type in nested interfaces

Having issues with generics and 'nested' interfaces when working with ReadonlyArray. Is there a way to utilize the filterList function without losing the type for the list parameter? interface INumber { value: number } interface IState { ...

Experimenting with HttpClient request using callFake() method

I am currently facing a challenge while trying to devise a spec for testing a method within my Angular service that initiates a GET request. The main issue I'm encountering is how to simulate the method returning an error instead of the expected respo ...

What steps should be taken to guarantee that the view is created only after receiving values from the route params subscription?

How can I ensure that the View is only rendered after the subscription has received values? When clicking on the Edit button in MyComponent_1, Angular navigates to MyComponent_2. In MyComponent_2, the view contains a form whose values are dependent on rout ...

Arranging elements within ngFor loop in Angular 2: positioning and aligning items

I am currently deepening my knowledge of Angular2. I am working on binding array values to the UI using ngFor and ngSwitch directives. I am aiming to display records based on various ngSwitchCase scenarios rather than just following the order in which they ...

Angular 4: How to Format Date Input Types

When using the item edit option, I encountered a form with a specific date format. How can I modify the date format to display as dd/MM/yy instead of the full year? For example, I want it to show 07/07/18 but currently it is displaying the full year. Wha ...

Issue found in ng-bootstrap.js ng-bootstrap for Angular 6

Recently, I added ng-bootstrap to my Angular project and included its modules. However, upon checking the CLI, an error was displayed. "WARNING in ./node_modules/@ng-bootstrap/ng-bootstrap/fesm5/ng-bootstrap.js 9853:57-75 "export 'ɵɵdefineInj ...

`transpilePackages` in Next.js causing Webpack issue when used with Styled Components

I'm encountering an issue while utilizing components from a custom UI library in a repository. Both the repository and the web app share the same stack (React, Typescript, Styled Components) with Next.js being used for the web app. Upon running npm ru ...

What causes the left click to not trigger in Kendo's Angular Charts?

My homepage features a simple bar chart that displays correctly, but I am having trouble capturing the left click event (the right click works fine). This is an example of code from my template: <kendo-chart *ngIf="(dataExists | async)" [ ...

An error occurred with the datepicker: Unable to connect to 'bsValue' as it is not recognized as a property of 'input'

Despite importing DatepickerModule.forRoot() in my Angular unit test, I am encountering the following error: Error: Template parse errors: Can't bind to 'bsConfig' since it isn't a known property of 'input'. (" ...

Developing applications using the combination of Vue.js, JSX, and

While I have no issues using TypeScript with single file components (.vue files), I encountered problems when attempting to use it with JSX files. Two errors arise, one in my index.ts file. I'm uncertain if there was a mistake made in my configuration ...