Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get Form Data in Angular 2

Tags:

I have a form in my angular 2 Project.

I know how to Retrieve data from the API. But don't know how to Perform a CRUD operation in there.

Can anybody help me with the simple codes on How to Send form data in JSON format to a Web Service in PHP/Any other Language...

like image 786
Deepak swain Avatar asked Dec 17 '16 05:12

Deepak swain


People also ask

How do I get Angular form data?

When you need Angular to access your data from forms, add ngModel to that tag as shown above. Now, if you want to read the email address and password, add ngModel for that field. You can see ngForm added to #userlogin . Now, add the ngForm directive to this form.

How do you get data from forms?

The method attribute specifies how to send form-data (the form-data is sent to the page specified in the action attribute). The form-data can be sent as URL variables (with method="get" ) or as HTTP post transaction (with method="post" ). Notes on GET: Appends form-data into the URL in name/value pairs.

What is form data in Angular?

When you use the FormData API, you can create a compilation of key/value elements that correspond to the contents of form fields and their values. Angular 13 HttpClient can then be used to send this data to the server.

How do I get data from form event?

Grabbing data from a FormData object If you want to snitch into a FormData object visit the example HTML form in a browser and place a breakpoint on console. log(event. formData) . Fill and submit the form with the browser's console opened and save the object as a global variable.


1 Answers

In Angular 2+ we handle forms two ways:

  • Template-driven
  • Reactive

Here I am sharing code for simple template-driven forms. If you want to do it using reactive forms then check this link: Angular2 reactive form confirm equality of values

Your module file should have these:

import { platformBrowserDynamic } from '@angular/platform-browser-dynamic' import { ReactiveFormsModule, FormsModule } from '@angular/forms'; import { MyApp } from './components'  @NgModule({   imports: [     BrowserModule,     FormsModule,     ReactiveFormsModule   ],   declarations: [MyApp],   bootstrap: [MyApp] }) export class MyAppModule {  }  platformBrowserDynamic().bootstrapModule(MyAppModule) 

Simple registration html file:

<form #signupForm="ngForm" (ngSubmit)="registerUser(signupForm)">   <label for="email">Email</label>   <input type="text" name="email" id="email" ngModel>    <label for="password">Password</label>   <input type="password" name="password" id="password" ngModel>    <button type="submit">Sign Up</button> </form> 

Now your registration.ts file should be like this:

import { Component } from '@angular/core'; import { NgForm } from '@angular/forms';  @Component({   selector: 'register-form',   templateUrl: 'app/register-form.component.html', }) export class RegisterForm {   registerUser(form: NgForm) {     console.log(form.value);     // {email: '...', password: '...'}     // ... <-- now use JSON.stringify() to convert form values to json.   } } 

To handle this data on server side use this link: How to post json object with Http.post (Angular 2) (php server side). I think this is more than sufficient.

like image 56
Amit kumar Avatar answered Sep 21 '22 12:09

Amit kumar