私は角度2のフォームを持っています。symfony API Webサービスを使用してデータベースに保存したいのですが、角度フォームからデータをフェッチしようとしています。ポストhttpリクエスト経由のURLエンドポイント。角度2のフォームデータをsymfony APIにポスト
これは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>
サービスです:
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());
}
}
、ここ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')
);
}
はsymfonyですWebサービス:
/**
* @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;
}
私はそれがnullオブジェクトを返すデータ値をダンプしようとします!助けてください!
'savePostEvaluationService()'に渡す前に 'savePostEvaluation()'の 'form'に値がありますか? 'ngModel'を任意のオブジェクトプロパティにバインドしているようには見えません。 [forms](https://angular.io/guide/forms)のドキュメントを参照してください。 2つのバインディングは '[(ngModel)] =" someModel.propertyName "'のようになります。バインディングを更新し、送信時に表示されている内容に関する詳細情報を追加します。 –
ハンクスの答えを共有するAlexander :)しかし、私はjsonオブジェクトのようなフォームを{label:label、value:value}というWebサービスに送信してデータベースに保存したいと思っています! - – sahnoun