1

我也是 angular 和 firebase 的新手,所以我很难用 firebase 承诺数据填充我的表单。是的,我也很难理解如何处理 promise 与 observables。我会感谢任何帮助。

ngOnInit() {

this.form = this.fb.group({
        'email': ['', [ Validators.required,
                      Validators.pattern('^[a-z]+[a-z0-9._]+@[a-z]+\.[a-z.]{2,5}$')]],
        'name': ['', [ Validators.required, Validators.minLength(8)]],
        'country':  ['', [ Validators.minLength(8)]],
        'state':  ['', [ Validators.minLength(8)]],
        'city':  ['', [ Validators.minLength(8)]],
        'sexualOpt':  ['', [ Validators.minLength(8)]],
        'marital':  ['', [ Validators.minLength(8)]],
        'dateOfBirth':  ['', [ Validators.minLength(8)]],
        'noChildren':  ['', [ Validators.minLength(8)]],
        'schoolar':  ['', [ Validators.minLength(8)]]
});
this.readProfile() ;}



readProfile() {
firebase.database().ref( 'User/'  + 'xs9XP4Aq2aVt0q5CkCuocrYyHwC3' + '/profile'  )
         .once('value')
         .then (function(snapshot) {
          this.form.controls['name'].setValue(snapshot.val().name);
          console.log(snapshot.val());
         });
;}

控制台显示:错误:访问属性“拒绝”的权限被拒绝

问候

4

1 回答 1

0

您可以使用初始值创建表单:

fields.forEach(field => {
  this.form.addControl(field.name,
    new FormControl({
      value: formValues[field.name],
      disabled: field.read_only || this.isFormDisabled()
    }, this.getValidators(field.type)));
});

或者像你一样设置它:

this.survey.controls['account'].patchValue(survey.account);
this.survey.controls['account'].setValue(survey.account);

记录您的数据console.log('snapshot', snapshot)查看您收到的内容,而不是正确使用数据。

于 2017-08-27T20:50:03.867 回答