Angular 2: Store all form inputs within a JSON object upon submission

I am working on a form that has multiple fields and I need to retrieve the data once it is submitted.

This is the code in component.html :

<div class="ui raised segment">
  <h2 class="ui header">Demo Form: Sku</h2>
  <form #f="ngForm"
        (ngSubmit)="onSubmit(f.value)"
        class="ui form">

    <div class="field">
      <label for="skuInput">SKU</label>
      <input type="text"
             id="skuInput"
             placeholder="SKU"
             name="sku" ngModel>
    </div>
    <div class="field">
      <label for="select1" class="col-md-4 col-lg-4 col-sm-2 align form-control-label">Languages: French/English: </label>
      <select class="form-control" name="note" id="select1" ngModel>
        <option>1</option>
        <option>2</option>
        <option>3</option>
        <option>4</option>
        <option>5</option>
      </select>

      ...
      
    </div>

    <button type="submit" class="ui button">Submit</button>
  </form>
</div>

And here is the code in component.ts :

import {Component, OnInit} from '@angular/core';

@Component({
  selector: 'app-post-history',
  templateUrl: './post-history.component.html',
  styleUrls: ['./post-history.component.css']
})
export class PostHistoryComponent implements OnInit {

  constructor() {
  }

  ngOnInit() {
  }

  onSubmit(form: any): void {
    console.log('You have submitted the following values:', form);
  }

}

When checking the console log, it only shows the text input and one select value being captured!

https://i.sstatic.net/Lahy6.png

Answer №1

All your select components share the identical name attribute "note." It may be helpful to update them to correspond with the element ids:

<select class="form-control" name="note1" id="select1" ngModel>

<select class="form-control" name="note2" id="select2" ngModel>

// and so on

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

Attempting to fetch a JSON object from PHP to Android

I'm currently working on parsing an array from my PHP script to my Android app. Below is a snippet of my Android code: public void func4(View view) throws Exception { final String TAG_CONTACTS = "response"; AsyncHttpClient client = new AsyncH ...

Tips for showing images with the full path URL retrieved from JSON using AngularJS

I am currently working on a project that involves displaying images from a JSON file. The URLs of these images are stored in the JSON file. Currently, my code is only outputting the URLs themselves, which is expected. However, I am looking for a way to act ...

Convert the output of get_post_meta() into a JSON format

I am seeking assistance in converting the generated data from get_post_meta into JSON format. The array that was generated looks like this: Array ( [0] => Array ( [0] => Array ( [0] => A ...

Utilize jQuery to transform array values into date objects

I am receiving an array from a .net controller. The values I am getting for dates are: /Date(1445256000000)/ and /Date(1445256900000)/ Instead of this, I need to convert these into proper date values. Now that I have an array of objects, I want to upda ...

Encountered error message: "Cannot assign argument of type '() => () => boolean' to parameter of type 'EffectCallback'"

I recently started working with TypeScript. I encountered an issue when attempting to utilize useEffect in TypeScript within a React context, Error: Argument of type '() => () => boolean' is not assignable to parameter of type 'Effec ...

Moshi: Difficulties encountered with serializing a List of MyClass instances

As I transition from Gson to Moshi, I encounter difficulties in serializing List<object>. While regular objects pose no issue, Lists seem to present a challenge. Within my ImageDTO class, equipped with a Moshi adapter, the structure is as follows: c ...

Keycloak is having trouble interpreting the value returned by my ID provider. How can I resolve this issue?

I'm currently integrating Keycloak with an external OAuth server serving as the identity provider. During the login process, Keycloak initiates an authentication backchannel request and expects a JWT response from the OAuth server. However, when att ...

Deciphering elements within an array using Node.js

After receiving a JSON code from a database, I am faced with the challenge of parsing an object within an array. The specific structure of the code is proving to be a hurdle in my efforts. [ { name: 'John1', surname: 'Doe1' }, { name ...

Exploring ways to access a nested JSON object | Showing Orders in a Laravel E-commerce Platform

{"**6732987ae9ac9ac3d465ea993bf9425c**": {"rowId":"6732987ae9ac9ac3d465ea993bf9425c","id":14,"name":"Stanley Metz","qty":2,"price":2039,"weight":550,"options&quo ...

The dependencies of Nest are unable to be resolved by the system

Attempting to implement AuthService within UsersService and UsersService within AuthService results in a "circular dependency" issue. The error message states that "Nest can't resolve dependencies of the AuthService (UserModel, JwtService, ?). Please ...

What is the best way to merge three arrays of data into a single iterable array?

I am working on a function that fetches data from an API. In this process, I execute a total of 3 queries, each returning an array of data. My goal is to combine these 3 arrays into a single iterable array, where the indexes correspond to the original a ...

Switching buttons with AngularJS

I am currently working on a Github search app using the Github API in Angular. My goal is to make it so that when the user clicks the "Add to Favorite" button, the button disappears and the "Remove Favorite" button is displayed instead. I attempted to achi ...

Issue with PHPMailer on localhost; Error 503 received after clicking "submit" on the domain, email delay of 4 minutes experienced

I've been troubleshooting this issue for two days now. The code runs smoothly on my xampp Apache localhost, sending emails immediately. However, when I try to submit the form on my domain, it just keeps loading and eventually times out with a "503 Ser ...

Slider in Angular Material not moving along the track

When I attempt to slide the mat-slider control using the mouse, it doesn't move left or right. Instead, it only responds when I click on a specific point along the sliding line. Here is the code snippet I am using: <div> <mat-slider>& ...

Generating PDF files from HTML documents using Angular

I am currently working on an Angular 11 application and I have a specific requirement to download a PDF file from a given HTML content. The challenge is that the HTML content exists independent of my Angular app and looks something like this: < ...

The Sequence of Import Statements in Angular 2

The Angular Style Guide provides recommendations on Import line spacing: It is suggested to include one empty line between third-party imports and application imports. Consider arranging import lines in alphabetical order based on the module. For destruc ...

Raycaster in Three.js fails to recognize clicks on objects

Having trouble detecting clicks on Three.js objects using a raycaster in Angular? Here's the code I'm working with: @Component({ selector: 'app-viewer-3d', templateUrl: './viewer3d.component.html', styleUrls: ['./vi ...

Guide to designing a Parcelable class from the provided JSON Data

Is there a way to create a parcel class when the array key changes every time you call the API? { "status": 200, "message": "Ground Schedules", "data": { "Schedules": { "2017-05-04": [ ...

What is the best way to iterate through a list of values stored in a form group's array?

I am facing an issue with my form group declaration. Below is the code snippet: this.secondFormGroup = this._formBuilder.group({ nested: this._formBuilder.group({ arr1: [], arr2: [], arr3: [], arr4: [] }), }) After the user fi ...

How can the Jackson structure of a class be obtained during serialization process?

Is it feasible for Jackson to generate a comprehensive list of the necessary fields for serializing and deserializing any given class? Jackson's process for serialization can be intricate. My objective is to dynamically determine the expected JSON st ...