How to pass the array values in HTTP POST method - html

I have the code like this:
empty values getting passing array object.
when log the details it's showing fine.
complaint-reg.component.html:
<div class="col-md-6 panel panel-default">
<h2>Complaint registration</h2>
<form #complaintRegForm="ngForm" (ngSubmit)="createComplaint(complaintRegForm.value)">
<div class="form-group">
<label>District:</label>
<input type="text" class="form-control" name="District" ngModel>
</div>
<div class="form-group">
<label>Subject:</label>
<input type="text" class="form-control" name="Subject" ngModel>
</div>
<div class="form-group">
<label>Description:</label>
<input type="text" class="form-control" name="Description" ngModel>
</div>
<div class="form-group">
<label>Place of occurence:</label>
<input type="text" class="form-control" name="PlaceOfOccurence" ngModel>
</div>
<div class="form-group">
<label>Name:</label>
<input type="text" class="form-control" name="Name" ngModel>
</div>
<div class="form-group">
<label>Gender:</label>
<input type="radio" class="form-control" name="Male" ngModel>Male
<input type="radio" class="form-control" name="Female" ngModel>Female
</div>
<div class="form-group">
<label>Address:</label>
<input type="text" class="form-control" name="Address" ngModel>
</div>
<div class="form-group">
<label>Mobile no:</label>
<input type="number" class="form-control" name="MobileNo" ngModel>
</div>
<div class="form-group">
<label>Email:</label>
<input type="text" class="form-control" name="Email" ngModel>
</div>
<div class="form-group">
<label>Date of occurence:</label>
<input type="text" class="form-control" name="DateOfOccurence" ngModel>
</div>
<button type="submit" class="btn btn-primary">Submit</button>
</form>
</div>
complaint-reg.component.ts:
import { Component, OnInit } from '#angular/core';
import { ComplaintRegistrationService } from './complaint-registration.service';
#Component({
selector: 'app-complaint-registartion',
templateUrl: './complaint-registartion.component.html',
styleUrls: ['./complaint-registartion.component.css'],
providers:[ComplaintRegistrationService]
})
export class ComplaintRegistartionComponent implements OnInit {
constructor(private complaintCreation : ComplaintRegistrationService) { }
ngOnInit() {
}
complaintObj = {};
createComplaint(Obj){
console.log(Obj);
this.complaintCreation.createCompliants(Obj).subscribe();
}
}
and
complaint-reg.service.ts:
import { Injectable } from '#angular/core';
import { Http,Response } from '#angular/http';
import 'rxjs/add/operator/map';
#Injectable()
export class ComplaintRegistrationService{
private _url:string ="http://192.168.0.106:8000/app/complaint/create"
constructor(private _http:Http){}
createCompliants(complaintObj){
return this._http.post(this._url,complaintObj).map((response:Response)=>console.log(response));
}
}
passing empty values. Attaching screen

First of all, I want to point out, that what you are sending and what you are console logging, is NOT the same. Actually what you are logging what the backend is sending you, not what it has received, so your post request is actually probably sending data, it is just in the "wrong format".
Looking at your console, it seems that the problem is that you are sending the properties with upper camel case, but the backend expects you using lower camel case.

You didn't bind input elements to the ngModel. Add binding to input controls like this
<input type="text" class="form-control" name="District" [(ngModel)]="complaintObj.district" >
when you submit the form complaintObj should not be empty.

Related

INPUT DATE VALUE ANGULAR

SERVICE.TS
addP(nome: string, cognome: string, anno_n: string): Observable<any> {
return this.http.post<Partecipanti>(this.partecipantiUrl, {
nome: nome,
cognome: cognome,
anno_n: anno_n
}, this.httpOptions).pipe(
tap((newPartecipante: Partecipanti) => this.log(`partecipante aggiunto w/ id=${newPartecipante.id}`)),
catchError(this.handleError<Partecipanti>('addP'))
);
}
COMPONENT.TS
add(nome: string, cognome: string, anno_n: string): void {
this.PartecipantiService.addP(nome, cognome, anno_n).subscribe(res => {
console.log(res);
this.loadUser();
})
}
<form class="row row-cols-lg-auto g-3 align-items-center float-center" (ngSubmit)="add(nome.value, cognome.value, anno_n.value)" (ngSubmit)="loadUser()" style="justify-content: center;">
<div class="col-12">
<label class="visually-hidden" for="inlineFormInputGroupUsername">Nome</label>
<div class="input-group">
<input type="text" class="form-control" id="inlineFormInputGroupUsername" required #nome placeholder="Nome">
</div>
</div>
<div class="col-12">
<label class="visually-hidden" for="inlineFormInputGroupUsername">Cognome</label>
<div class="input-group">
<input type="text" class="form-control" id="inlineFormInputGroupUsername" required #cognome placeholder="Cognome">
</div>
</div>
<div class="col-12">
<label class="visually-hidden" for="inlineFormInputGroupUsername">Data di nascita</label>
<div class="input-group">
<input type="text" class="form-control" id="inlineFormInputGroupUsername" required #anno_n useValueAsDate placeholder="Data di nascita (GG/MM/AAAA)">
</div>
</div>
<div class="col-12">
<button type="submit" class="btn btn-primary shadow-lg">Salva</button>
</div>
</form>
How can I take the date value in my function "add"?
In my function add I have 3 field (name, surname and date), when I call my function add on a button I use (ngSubmit)="add(name.value, surname.value, date...?). What I have to use? Value is for string, I can't find something for Date! This part is on component.html Can you explain me how it works? My input is type="date"
I would suggest you use ngForm. This is the easiest for handling this scenario.
See here the Documentation: https://angular.io/api/forms/NgForm
Here is a working example i did: https://stackblitz.com/edit/angular-ivy-pqgewm?devtoolsheight=33&file=src/app/app.component.html
In Essence you want your HTML look like this:
<form #form="ngForm" (ngSubmit)="userService.submitForm(form)">
<div>
<label for="firstName">First Name</label>
<input name="firstName" id="firstName" type="text" ngModel required />
</div>
<div>
<label for="lastName">Last Name</label>
<input name="lastName" id="lastName" ngModel type="text" required />
</div>
<div>
<label for="dateOfBirth">Date of Birth</label>
<input name="dateOfBirth" id="dateOfBirth" ngModel type="date" required />
</div>
<br />
<button type="submit">Submit</button>
</form>
And your Submit function to look like this:
submitForm(form: NgForm) {
if (form.valid) {
const user: User = {
name: form.value['firstName'],
surname: form.value['lastName'],
birth: new Date(form.value['dateOfBirth']),
};
}
}

bind keyup.enter with tab using angular

do someone knows how i can bind the keyup.enter proproty to the tab of the keyboard?
i mean i want that when i enter the value in a first field of my form that i spring in the second field.
Like:
<form>
<label for="xxx">first:
<input type="text" id="first" name="first" (keyup.enter)="enterFirst(firstName)">
</label>
<label for="xxx">second:
<input type="text" id="second" name="second" (keyup.enter)="enterSecond(lastName)">
</label>
.
.
.
<button type="button" class="btn btn-success" (click)="sendNames(xxx)">valid</button>
</form>
So i would to be able to spring on the field second when i give the firstname and click on enter.
Do someone have an idea? thanks in advance
This should work, although it is vanilla JS:
<input type="text" id="first" name="first" onkeyup="if (event.keyCode == '13') {enterFirst(this)}">
(EDITED: Replaced "firstName" with "this")
HTML:
<h1>Here we will try to bind the keyup.enter with Tab</h1>
<div class="container">
<h2>Please enter datas an click on enter</h2>
<form>
<div class="row">
<div class="form-group col">
<label for="fname">Enter your firstname:</label>
<input type="text" class="form-control" placeholder="enter your firstname and click enter"
id="fname" name="fname" onkeyup="if (event.keyCode == '13') {enterFirst(this)}">
</div>
<div class="form-group col">
<label for="lname">Enter your lastname:</label>
<input type="text" class="form-control" placeholder="enter your lastname and click enter"
id="lname" name="lname" onkeyup="if (event.keyCode == '13') {enterLast(this)}">
</div>
</div>
<button type="button" class="btn btn-success" (click)="sendData()">submit</button>
</form>
</div>
Ts:
import { Component, OnInit } from '#angular/core';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent {
title = 'angulartest';
public firstname: string;
constructor(){}
ngOninit(){
}
enterFirst(firstname){
console.log(firstname);
}
sendData(){
}
}
I wrote it just to see if i can spring to the field lastname by clicking on enter after the firstname

Pass data from HTML page to typescript file

I have a ngForm for singup page. I have tried passing the data to the .ts file but failed to print them on console.
Here's my code:
<div class="column" style="padding: 7.5%">
<form #instituteForm="ngForm" (ngSubmit)="instituteLogin(instituteForm)">
<div class="form-group">
<label> Full Name </label>
<input
type="text"
ng-maxlength="10"
hint="Full Name"
name="patient"
id="name"
class="form-control"
[(ngModel)]="institute.patient">
<label> Phone Number </label>
<input
type="number"
hint="phone"
name="phoneno"
id="phone"
maxlength="10"
minlength="10"
class="form-control"
[(ngModel)]="institute.phoneno">
<label> Aadhar Number </label>
<input
type="number"
hint="aadhar"
id="aadhar"
name="aadhar"
maxlength="16"
minlength="16"
class="form-control"
[(ngModel)]="institute.aadhar">
<br><br>
</div>
<button id="signup" class="btn btn-primary" type="submit">Signup</button>
</form>
</div>
institute = {
patient: '',
phoneno: '',
aadhar: ''
};
instituteLogin(instForm: NgForm) {
console.log("Entered new patient");
console.log(instForm.value);
}
You have not added the FormsModule in app.module.ts.
import { FormsModule } from '#angular/forms';
#NgModule({
imports: [
// Other imports
FormsModule
]
})
Working demo : https://stackblitz.com/edit/angular-ybbamr
In this case it should be like this:
instituteLogin(): void{
console.log("Entered new patient");
console.log(this.institute);
}
Well, I think you should use FormControlName instead of ngModel.
html:
<div class="column" style="padding: 7.5%" >
<form [formGroup]="institute" #instituteForm (ngSubmit)="instituteLogin()">
<div class="form-group">
<label> Full Name </label>
<input type="text" ng-maxlength="10" hint="Full Name" id="name" class="form-
control" formControlName="patient">
<label> Phone Number </label>
<input type="number" hint="phone" id="phone" maxlength="10" minlength="10"
class="form-control" formControlName="phoneno">
<label> Aadhar Number </label>
<input type="number" hint="aadhar" id="aadhar" maxlength="16" minlength="16"
class="form-control" formControlName="aadhar">
<br><br>
<button id="signup" class="btn btn-primary"
routerLink="../../home/dashboard">Signup</button>
</div> </form>
</div>
ts:
institute:FormGroup;
instituteLogin(){
console.log("Entered new patient");
console.log(this.institute.value);
}
constructor(private formBuilder:FormBuilder) { }
ngOnInit() {
this.institute =this.formBuilder.group( {
patient: new FormControl(),
phoneno:new FormControl(),
aadhar:new FormControl()
});
}

How to convert an object to JSON list in Angular 2?

I am creating a form which outputs JSON. I need some fields as a JSON list.
For eg:
{"Name":"sam","age":"21","Friends":[{"Name":"bob","age":"21"}]}
But, when I use this code JSON.parse(JSON.stringify(value))
I end up getting something like this
{"Name":"sam","age":"21","Friends":{"Name":"bob","age":"21"}}
The [] is missing. How do I make a JSON list?
ps: I am new to web development, forgive me if the question is silly.
my app.component.html
<div class="container">
<form #payloadForm="ngForm" (ngSubmit)="onSubmit(payloadForm.value)">
<div class="form-group">
<label>Name</label>
<input type="text" name="Name" ngModel value="" class="form-control">
</div>
<div class="form-group">
<label>age</label>
<input type="text" name="age" ngModel value="" class="form-control">
</div>
<div ngModelGroup="Friends">
<label>Friend Name</label>
<div class="form-group">
<label>Name</label>
<input type="text" name="Name" ngModel value="" class="form-control">
</div>
<div class="form-group">
<label>age</label>
<input type="text" name="age" ngModel value="" class="form-control">
</div>
</div>
<button type="submit" class="btn btn-primary" name="button">Submit</button>
</form>
</div>
my app.component.ts
import { Component } from '#angular/core';
#Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent {
onSubmit(value: any){
console.log(JSON.parse(JSON.stringify(value)));
console.log("String");
console.log(JSON.stringify(value));
}
}
you can JSON.parse() that complete JSON string. After that you can simply access the array from that object you got returned from JSON.parse().

HTML Input value in angular2

I am working on a an angular2 project and i setted the value of html element (input) value to 0 as follows
<div class="form-group">
<label for="balance">Balance</label>
<input ngModel type="number" class="form-control" id="balance" placeholder="Balance" name="balance" value="0" required>
</div>
but when i run the project i get empty input.
any help?
<div class="form-group">
<label for="balance">Balance</label>
<input type="number" class="form-control" id="balance" placeholder="Balance" name="balance" [(ngModel)]="balance" required>
</div>
In your component constructor
balance:number;
constructor(){
this.balance = 0;
}