I am working on edit functionality in angular 14. My requirments are bit diff thats why i am not using set value to assign value in form . I have get the value from api in the form of model
export class EditCandidateComponent implements OnInit {
candidateData: any;
response: any;
editCandidateSubmitted = false;
data = new Candidate();
constructor(private route: ActivatedRoute, private candidateService: CandidateService) { }
ngOnInit(): void {
this.getCandidate(this.route.snapshot.params.id);
}
getCandidate(id:any)
{
this.candidateService.getCandidate(id).subscribe(
(resp: Candidate) => {
this.setformValue(resp);
},
(err: any) => {
console.log(err+'new');
}
);
}
setformValue(updateValue) {
this.data = updateValue;
console.log(this.data);
}
addCandidate = new FormGroup({
first_name: new FormControl(this.data.first_name, [
Validators.required,
Validators.pattern(AppConstants.OnlyAlphabetic),
]),
last_name: new FormControl(this.data.last_name, [
Validators.required,
Validators.pattern(AppConstants.OnlyAlphabetic),
]),
});
get candidateFormControl() {
return this.addCandidate.controls;
}
onSubmit() {
}
Here is my http call with model in return
return this.http.get<any>(slug, {observe: "response"}).pipe(map(
(res) => {
return new Candidate(
res.body.data['first_name'],
res.body.data['last_name'],
);
})
);
I have tried set value but I am not using it because I have diff requirments. I want to show th value in formgroup. When i tried to move formgroup in setformValue function it give my error so how i can do this.
Here is the model
export class Candidate {
constructor(
public first_name : string = '',
public last_name : string = '',
) {}
}