【问题标题】:How to make sure the user enter an image (FORM ANGULAR)如何确保用户输入图像(FORM ANGULAR)
【发布时间】: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


    【解决方案1】:

    我正在尝试另一种方式,例如: 仅当字段 item.picture 不为空但它不起作用时才上传图像,因为我在上传时需要项目的密钥(并且我不想在它输入图像之前上传它..)

    因此,如果有人知道它的存在,将不胜感激一种表单控件

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-05-17
      • 2020-10-30
      • 2021-12-09
      • 1970-01-01
      • 1970-01-01
      • 2018-06-08
      相关资源
      最近更新 更多