Converting an array of form data into JSON using Angular and Typescript

When submitting a form using ng-submit in my Angular application, I receive an array of fields that I need to post to a RESTful web service. However, I'm unsure how to map and convert this data into the desired final object template within my Angular Typescript controller.

Do you have an example or tutorial on how to achieve this? I've searched but couldn't find anything helpful. It seems like I need to declare and create an object with the structure of the JSON I want, but I'm unsure how to properly map all the fields.

What is the most efficient way to send an Angular form with specific fields to another JSON template?

Thank you for any assistance.

Answer №1

If you want to pass your form data directly to the controller in object format, you can do so by creating an empty object in the controller like this: $scope.formData = {};. Within your HTML form, make sure to include the following:

<form ng-submit="submitData(formData)">
  <input type="text" ng-model="formData.field1">
  <input type="text" ng-model="formData.field2">
  <button type="submit">Submit</button>
</form>

By setting up your form and controller in this way, your data will be structured as an object when it is submitted. I hope this explanation has been useful.

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

AngularJS - Strange bug encountered when transferring and removing JSON data from cache within a loop structure

The loop is exhibiting odd behavior where the alerts are triggering out of sequence and the JSON data is all being sent simultaneously. I can't figure out why this is happening. I've been grappling with this issue for a while now and any assistan ...

Decoding a JSON String

Are there any built-in libraries that can help with parsing a Json formatted string? For instance, if I have the following string: string input = "{ \"title\": \"My Title\" }"; Is there a specific class that allows me to create an obj ...

What is the best way to adjust the size of carousel images within a box element?

How can I ensure that carousel pictures are displayed clearly within the box without overflowing? The code I tried resulted in the pictures overflowing from the box and not being visible clearly. How can I resolve this issue? .container { width: 1490px; ...

What is the best method for sending a DbGeography parameter to MVC?

I am working on developing an API that allows users to save polygons on the server using ASP.NET MVC 5. Can anyone guide me on how to properly format the AJAX parameters for posting requests with DbGeography? This is what I have tried so far: $.ajax({ ...

Django now supports natural key serialization for the User model, making it

Imagine having a comment model in your project: class Comment(models.Model): text = models.TextField(max_length=500, blank=False) author = models.ForeignKey(User) When converting it to JSON using django.core.serializers, the author field appears ...

Nestjs is throwing an UnhandledPromiseRejectionWarning due to a TypeError saying that the function this.flushLogs is not recognized

Looking to dive into the world of microservices using kafka and nestjs, but encountering an error message like the one below: [Nest] 61226 - 07/18/2021, 12:12:16 PM [NestFactory] Starting Nest application... [Nest] 61226 - 07/18/2021, 12:12:16 PM [ ...

The Wordpress admin-ajax.php script is failing to process the function and returning a "0" error code

I have been experimenting with processing AJAX requests in Wordpress and I'm following a particular tutorial to achieve this. The goal is to create a basic AJAX request that will display the post ID on the page when a link is clicked. The Approach ...

It appears that TypeScript is generating incorrect 'this' code without giving any warning

I seem to be facing some resistance filing a feature request related to this on GitHub issues, so I'll give it a shot here. Here is the code snippet that caused me trouble: export class Example { readonly myOtherElement: HTMLElement; public ...

Discovering the Cookie in Angular 2 after it's Been Created

My setup includes two Components and one Service: Components: 1: LoginComponent 2: HeaderComponent (Shared) Service: 1: authentication.service Within the LoginComponent, I utilize the authentication.service for authentication. Upon successful authent ...

Tips for including headers from an HTML or UI into an API request to a server

I am looking for a way to include HTTP headers in all client requests, but so far I haven't been able to find a solution. I attempted to add the following script directly into the HTML to set headers: <script> res.setHeader('Authoriza ...

Accessing MongoDB through user authentication

After following the documentation, I successfully created an administrative user: use admin db.createUser( { user: "siteUserAdmin2", pwd: "password", roles: [ { role: "userAdminAnyDatabase", db: "admin" } ] } ) Next, I proceeded to create ...

Updating a value in Expressjs variable is not working as expected

In the code snippet below, I have declared a variable called sumOfRevenue. I assigned it a value of 10 in the router, but when I try to print its value, it comes out blank. Can you please help me understand why it's not showing as 10? Please review t ...

eliminate element from formBuilder

When I execute formBuild.group, I am creating two temporary values for validation purposes only. These values are not intended to be saved in the database, so I need to remove them before saving. profile.component.ts: profileForm: FormGroup; constructor ...

Is it possible to modify the class within TypeScript code while utilizing the CSS library for styling?

In my custom style sheet coustume-webkit.css, I have the following code snippet: .tabella .pagination > li:last-child > a:before, .tabella .pagination > li:last-child > span:before { padding-right: 5px; content: "avanti"; ...

Nuxt: Encountered an unexpected < symbol

https://i.sstatic.net/MbM9f.png I've been working on a nuxt project where I'm currently in the process of creating a map component using Google Maps with the help of the plugin https://www.npmjs.com/package/vue2-google-maps. After installing the ...

The $location.search parameter does not get updated immediately

The issue I am facing is that my URL does not update every time I set it. Strangely, this seems to be related to the directive, as it works in other cases. Therefore, I am wondering what the $location.search('dd', val) function is dependent on a ...

Error: Webpack module is not a callable function

I have been developing a backend system to calculate work shifts. I am currently facing an issue with posting user input using a module located in services/shifts. While the getAll method is functioning properly, I encounter an error when trying to post da ...

Creating a Dynamic List using Flutter for JSON Data

Currently, I am grappling with handling intricate JSON data in Dart and facing challenges with creating objects without prior knowledge of their types. Although I am grateful for the suggestions provided, I find myself still puzzled. For instance, in the ...

Ways to troubleshoot Null Error in JSON Decoding

I encountered an issue where my API URL returned code in the browser, but when I tried to use json_decode($api_url,true);, it returned null. After checking json_last_error();, I found that it was returning a JSON syntax error (error code 4). Interestingl ...

AngularJS UI Sortable - Swapping the content of a dropped item

<div ui-sortable="sortableSection" ng-model="mainInputs" class="first"> <div ng-repeat="(i, input) in mainInputs | orderBy: input.type"> <div class="alert alert-success rounded gradient" >{{ input.text }}</div> </di ...