How to get Form Data in Angular 2
Asked Answered
O

4

28

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...

Obduce answered 17/12, 2016 at 5:38 Comment(2)
check this link... #41154819Furthermost
@AmitSuhag, i want to know how to bind the form data by both click event and onSubmit method. And then how to stringify it. Can you help me with a whole package solution... It will be very helpful for me...Obduce
F
45

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.

Furthermost answered 17/12, 2016 at 7:28 Comment(2)
Awesome! Thanks so much for the help on thisTracietracing
can anyone help me on this: #59319700Sharie
B
2

I fixed this issue by passing user.value to submit method, please see the following example:

app.component.html

<form #user="ngForm" (ngSubmit)="onSubmit(user.value)">
  <input type="text" name="email" placeholder="Email" ngModel required>
  <input type="password" name="passwd" placeholder="Password" ngModel required>
  <input type="submit" value="Submit">
</form>

app.component.ts

import { Component } from '@angular/core';
@Component({
   selector: 'app-root',
   templateUrl: './app.component.html',
   styleUrls: ['./app.component.css']
})
export class AppComponent {
   constructor() { }
   onSubmit(data) {
      console.log("Email: " + data.email
                 +"Password: " + data.passwd);
   }
}
Benitabenites answered 4/12, 2020 at 16:41 Comment(0)
M
1
<form [formGroup]="formData" (ngSubmit)="onSubmit()">
Name<input type="text" formControlName="name"><br><br>
City<select formControlName="city">
    <option value="kkp">KKP</option>
    <option value="MKT">MKT</option>
</select><br><br>



-->

formData:FormGroup;

constructor() { }

ngOnInit() {
    this.formData = new FormGroup({
        name    : new FormControl(),
        city    : new FormControl()
    });
}
onSubmit(){ 

    console.log(this.formData.value);

}

Microelectronics answered 14/10, 2019 at 10:48 Comment(0)
L
1

Ways to get form value in Angular application

  1. Using formControlName (Reactive Form)
  2. Using document object
  3. Using local reference object
  4. Using ngModel (Template-driven Form)
  5. Using ViewChild
  6. Using Input native events

Click here to read in detail.

Leipzig answered 23/12, 2021 at 3:1 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.