【发布时间】:2020-04-06 17:07:24
【问题描述】:
我正在制作一个表格,这个表格包含文本(姓名、年龄……)和一张图片。 现在我可以通过使用 FormGroup 来确保文本是必需的,但我想确保表单只有在输入图像时才有效。
所以我尝试了这种方式:(HTML文件)
<input formControlName="photo" type="file" (click)="selectImage()">
以及 TS 文件:
ngOnInit() {
this.sellingSneakForm = new FormGroup({
photo: new FormControl('', [Validators.required]),
});}
功能:
selectImage() {
let actionSheet = this.actionSheetCtrl.create({
title: 'Add an image',
buttons: [
{
text: 'Take a picture from camera',
role: 'CAMERA',
handler: () => {
this.takePicture();
}
},
{
text: 'Choose from your gallery',
role: 'GALLERY',
handler: () => {
this.selectPicture();
}
},
{
text: 'Cancel',
role: 'cancel',
handler: () => {
}
}
]
});
actionSheet.present();}
takePicture() {
this.camera.getPicture({
quality : 95,
destinationType : this.camera.DestinationType.DATA_URL,
sourceType : this.camera.PictureSourceType.CAMERA,
encodingType: this.camera.EncodingType.JPEG,
targetWidth: 500,
targetHeight: 500,
}).then(imageData => {
this.base64Image = "data:image/jpeg;base64," + imageData; // imageData is a base64 encoded string
this.Picture = imageData; //this.Picture is passing the string to our DB
}, error => {
console.log("ERROR -> " + JSON.stringify(error));
});}
当我上传图片时它可以 100% 工作,但我的应用程序不需要图片来将项目推送到数据库……这是我想要的 FormGroup 和 FormControl ==> 确保用户输入了图片..
这样可以吗?
谢谢
【问题讨论】:
标签: angular firebase ionic-framework ionic3