1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
| import { Component, OnInit } from '@angular/core';
import { FormBuilder, FormGroup, Validators } from '@angular/forms';
import {HttpClient, HttpResponse, HttpHeaders} from '@angular/common/http';
const httpOptions = {
headers: new HttpHeaders({
'Content-Type': 'application/json'
})
};
@Component({
selector: 'app-register-cmp',
templateUrl: './register-cmp.component.html',
styleUrls: ['./register-cmp.component.css']
})
export class RegisterCmpComponent implements OnInit {
active:boolean=true;
userObj:any;
formGroup: FormGroup;
email:string = '';
name:string = '';
password:string = '';
surname:string = '';
post:any; //A Property for our submitted form
titleAlert:string ='';
constructor(private fb: FormBuilder, private http: HttpClient) {
this.formGroup = fb.group({
'email':[null, Validators.compose([Validators.required, Validators.email])],
'surname':[null, Validators.compose([Validators.required, Validators.minLength(3)])],
'name':[null, Validators.compose([Validators.required, Validators.minLength(2), Validators.maxLength(20)])],
'password':[null, Validators.compose([Validators.required, Validators.minLength(4), Validators.maxLength(30)])],
'validate' : ''
});
}
ngOnInit() {
this.formGroup.get('validate').valueChanges.subscribe(
(validate)=>{
if(validate == '1'){
this.formGroup.get('name').setValidators([Validators.required, Validators.minLength(2), Validators.maxLength(20)]);
this.formGroup.get('surname').setValidators([Validators.required, Validators.minLength(3)]);
this.formGroup.get('email').setValidators([Validators.required, Validators.email]);
}
this.formGroup.get('name').updateValueAndValidity();
this.formGroup.get('surname').updateValueAndValidity();
this.formGroup.get('email').updateValueAndValidity();
}
);
}
addPost(f){
this.userObj =[{
"name": f.name,
"email": f.email,
"surname": f.surname,
"password": f.password
}]
this.http.post(`http://localhost:3000/add/`, this.userObj,httpOptions).subscribe((res:Response) => {
console.log(res);
})
} |
Partager