import { Component, OnInit } from '@angular/core';
import { FormControl, FormGroup, Validators } from '@angular/forms';
import { User } from '../account.interface';
import { AF } from '../angularfire.service';
@Component({
selector: 'app-account',
templateUrl: './account.component.html',
styleUrls: ['./account.component.less']
})
export class AccountComponent implements OnInit {
public error: any;
user: FormGroup;
onSubmit() {
console.log(this.user.value, this.user.valid);
}
currentUserDetails = [];
firstName: string = '';
lastName: string = '';
email: string = '';
phone: string = '';
bio: string = '';
userID: string = '';
constructor(private afService: AF) {
// Get current user details and push to array. This is due to input values overwriting ngModels.
afService.getCurrentUserInfo().then(currentUserDetails => {
let currentUser = [];
currentUser.push(currentUserDetails);
this.firstName = currentUser[0].firstName;
this.lastName = currentUser[0].lastName;
this.email = currentUser[0].email;
this.phone = currentUser[0].phone;
this.bio = currentUser[0].bio;
this.userID = currentUser[0].userID;
}).then(() => {
})
}
// Update the user details with the form entry.
// updateUserEntry(firstName, lastName, phone, bio) {
// this.afService.updateUserEntry(this.userID, firstName, lastName, phone, bio).then(() => {
// location.reload();
// }).catch((error) => {
// this.error = error;
// console.log("error");
// });
// }
ngOnInit() {
this.user = new FormGroup({
fname: new FormControl(this.firstName, [Validators.required, Validators.minLength(3), Validators.maxLength(24)]),
lname: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(24)]),
phone: new FormControl('', [Validators.required, Validators.minLength(3), Validators.maxLength(24)]),
bio: new FormControl('', [Validators.required, Validators.minLength(5), Validators.maxLength(50)]),
});
}
}
我知道这可能是一个非常简单的修复,我只是没有看到它。我有我的反应式formControl
,ngOnInit
但我需要将预设值传递给每个输入(即this.firstName
,this.lastName
等)。当视图加载时,输入为空。
我相信这些值没有显示出来,因为在填充值之前正在加载表单。有想法该怎么解决这个吗?