我正在进行我的第一个角反应表单项目,我有fakedata作为用户数据服务和注册表单,我想获得表单值作为新的用户数据,并将其添加到我的假数据中,这是我做的
在Sign-UpComponent.ts 中
export class SignUpComponent implements OnInit {
SignUpForm: FormGroup;
constructor( private fb:FormBuilder) {
this.SignUpForm=this.fb.group({
email:[ "" ,[Validators.required ,Validators.minLength(5), Validators.email , Validators.pattern("^[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,4}$")]],
password:[ "" ,[Validators.required ,Validators.minLength(6)]]
}
}
onSubmit(){
let aaa=this.newUserData
console.log(aaa);
}
get newUserData(){
const newUser:User[]=[{
password:this.SignUpForm.controls.password.value,
email : this.SignUpForm.controls.email.value,
}]
return newUser;
}
直到这里console.log打印值为[{email:"aaa@gmail.com",password:123456}]
的newUser
然后我将newUser变量作为下面的传递给用户服务
export class UsersDataService {
data:User[]=[{
password:"123456",
email:"bbb@gmail.com",
}]
newUser:User[]
allUsers:User[]=[]
getDataInfo(){
this.allUsers=this.data.concat(this.newUser);
return this.allUsers;
}
constructor( SignUpComponent:SignUpComponent) {
this.newUser= SignUpComponent.newUserData
}
}
现在console.log (allUsers)
应该是[[{email:"bbb@gmail",password:123456}],[{email:"aaa@gmail",password:123456}]]
吗
但它显示没有值[[{email:bbb@gmail,password:123456}],[{email:"",password:""}]]
在SignupComponent中,我将newUser数据发送到服务
onSubmit(){
this.newUserData()
}
newUserData(){
let newUser:User={
password:this.SignUpForm.controls.password.value,
email : this.SignUpForm.controls.email.value }
this.UsersDataService.getNewUser(newUser);
}
然后我把它推送到用户服务中的数据[]
export class UsersDataService {
data:User[]=[{
password:"123456",
email:"aaa@gmail.com",
}]
constructor() {
}
getNewUser(newUser:User){
this.data.push(newUser);
}
getDataInfo(){
console.log(this.data);
return this.data;
}
}