离子相机:没有名称的表单控件的值访问器

Ionic Camera: No value accessor for form control with name

我在 Ionic 应用程序中使用 cordova-plugin-camera 插件。我设法让相机拍照并在位后显示它我收到此错误:

No value accessor for form control with name 'photo'

我想我可能没有完全理解 FormBuilder 的工作原理。这是我得到的:

register.html

<img *ngIf="register_form.controls.photo.value != ''" formControlName="photo" [src]="domSanitizer.bypassSecurityTrustUrl(register_form.controls.photo.value)" />

register.ts 文件

ionViewWillLoad() {
  this.register_form = this.formBuilder.group({
    ...
    photo: new FormControl('', Validators.required)
  });
}
...
onTakePicture(){
  ...
  this.camera.getPicture(options).then((imageData) => {
        const image: FormControl = (<any>this.register_form).controls.photo;
        image.setValue('data:image/jpeg;base64,' + imageData);
    }, (err) => {
  });
}

怎么了?

formControlName只能用于实现ControlValueAccessor的控件或directives。由于您使用的 img 未实现 ControlValueAccessor,因此您不能在此处使用。

更多信息请访问 - https://angular.io/api/forms/ControlValueAccessor