我有角2的形式,我想使用symfony API web服務將它保存在數據庫中,我試圖從角形式獲取數據並將其作爲json對象發送給通過發送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'綁定到任何對象屬性。請參閱[表格](https://angular.io/guide/forms)文檔。兩個綁定看起來像'[(ngModel)] =「someModel.propertyName」'。更新綁定並添加更多關於您在提交時看到的信息。 –
hanks分享答案亞歷山大:)但我想發送的形式如json對象那樣:{label:label,value:value}到一個web服務將它保存在數據庫中! - – sahnoun