【发布时间】:2018-09-27 09:11:09
【问题描述】:
HTML
<form #productForm="ngForm" ngSubmit)="createNewProduct(productForm)">
<div class="form-group ">
<input type="file" [(ngModel)]="model.files" name="files" #files="ngModel" class="form-control" id="files" multiple="">
</div>
<div class="form-group ">
<label for="productname">Name</label>
<input type="text" class="form-control" id="productname" required minlength="5" pattern="^[a-zA-Z0-9/,-. ]*$" maxlength="30" [(ngModel)]="model.productname" name="productname" #productname="ngModel">
</div>
<div class="form-group ">
<label for="sales">Sales price/rate</label>
<input type="text" class="form-control" id="sales" pattern="[0-9]+" required minlength="0" maxlength="10" [(ngModel)]="model.sales" name="sales" #sales="ngModel">
</div>
提交
component.ts
createNewProduct(productForm: NgForm) {
this.productService.save_user(json).subscribe(response => console.log('Inserted Successfully'),
error => console.log(error));
}
service.ts
save_user(product_data: any): Observable<any[]> {
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json',
}),
};
return this.http.post('http://localhost:8000/product/', product_data, httpOptions)
.map(response => response)
.catch(error => Observable.throw(error.statusText));
};
这里我提到了我的 component.ts 和 service.ts,以及我的 Html。我不知道如何上传图片。
【问题讨论】:
-
可以使用表单数据上传angular 2中的文件。
-
“这是我目前尝试过的”,什么不起作用?
标签: angular angular2-template angular2-forms