I have angular 2 form and I want to save it in the data base using symfony API web service, I'm trying to fetch data from angular form and sent it as json object to the url endpoint via post http request.
this is the 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">Langues:français/anglais: </label>
<select class="form-control" name="note1" id="select1" ngModel>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<label for="select2" class="col-md-4 col-lg-4 col-sm-2 align form-control-label">Langues:français/anglais: </label>
<select class="form-control" name="note2" id="select2" ngModel>
<option>1</option>
<option>2</option>
<option>3</option>
<option>4</option>
<option>5</option>
</select>
<label for="select3" class="col-md-4 col-lg-4 col-sm-2 align form-control-label">Langues:français/anglais: </label>
<select class="form-control" name="note3" id="select3" 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>
the service :
import { Injectable } from '@angular/core';
import {Http, Response, Headers, RequestOptions} from '@angular/http';
import { savePostEvaluationapi} from '../../constants/api.endpoints';
@Injectable()
export class EvaluationService {
http: Http;
constructor(protected _http: Http) {
this.http = _http;
}
savePostEvaluationService(postEvalform) {
const headers = new Headers({'Content-Type': 'application/json'});
const options = new RequestOptions({headers: headers});
const data = JSON.stringify(postEvalform);
return this.http.post("http://evaluation.dev/app_dev.php/api/savePostEvaluation", data, options).map((res: Response) => res.json());
}
}
and the component.ts
import {Component, OnInit} from '@angular/core';
import {EvaluationService} from '../../services/evaluation/evaluation.service';
@Component({
selector: 'app-eval-en-cours',
templateUrl: './eval-en-cours.component.html',
styleUrls: ['./eval-en-cours.component.css']
})
export class EvalEnCoursComponent implements OnInit {
constructor(private evaluationService: EvaluationService) {
}
ngOnInit() {
}
// savePostEvaluation(data: Object) {
// this.evaluationService.savePostEvaluation(data).subscribe((dataResponse) => {
// console.log('execute' + dataResponse);
// });
// }
savePostEvaluation(form: any): void {
this.evaluationService.savePostEvaluationService(form)
.subscribe(
data => data.json(),
() => console.log('done send form to api')
);
}
here is the symfony web service :
/**
* @Rest\View()
* @Rest\Post("/savePostEvaluation")
* @return Response
* @internal param Request $request
*/
public function savePostEvaluation(Request $request){
header("Access-Control-Allow-Origin: *");
$data = json_decode($request->getContent(), true);
dump($data);die;
}
when I try to dump the data value it return null object ! any help please !
forminsavePostEvaluation()have any value prior to passing tosavePostEvaluationService()? It doesn't look like you are bindingngModelto any object properties. See forms documentation. A two binding would look like[(ngModel)]="someModel.propertyName". Updated the bindings and add more information regarding what you are seeing on submit.