I am looking for a way to transfer a value between components so that I can switch from a list of candidates to another panel where I can edit the selected candidate.
Unfortunately, I encountered an error: ERROR TypeError: "this.listCandidateComponent is undefined" in my edit-candidate component when trying to log the candidate initialized in list-candidate component.
list-candidate.component.html
<table class="table table-striped">
<tbody *ngFor="let candidate of candidates">
<td><h4>{{ candidate.id }}</h4></td>
<td><a class="btn btn-outline-warning btn-sm" style="margin: 1%"
(click)="getCandidateById(candidate.id)" role="button">Modifier</a>
</td>
</tbody>
</table>
list-candidate.component.ts
@Component({
selector: 'app-list-candidate',
templateUrl: './list-candidate.component.html',
styleUrls: ['./list-candidate.component.scss']
})
export class ListCandidateComponent implements OnInit {
candidate: Candidate;
candidates: Candidate[];
ngOnInit() {
this.getCandidateList();
}
async getCandidateById(id: number) {
const headers = new HttpHeaders({
'Content-type': 'application/json; charset=utf-8',
Authorization: 'Bearer ' + this.cookie.get('access_token')
});
const options = {
headers
};
await this.httpClient.get(`${this.baseUrl}/${id}`, options)
.toPromise()
.then(
(response: Candidate) => {
console.log('GET request successful', response);
this.candidate = response;
},
(error) => {
console.log('GET error : ', error);
}
);
await this.router.navigate(['/candidates/edit']);
}
edit-candidate.component.ts
@Component({
selector: 'app-edit-candidate',
templateUrl: './edit-candidate.component.html',
styleUrls: ['./edit-candidate.component.scss']
})
export class EditCandidateComponent implements OnInit, AfterViewInit {
candidate: Candidate;
@ViewChild(ListCandidateComponent) listCandidateComponent;
ngAfterViewInit() {
this.candidate = this.listCandidateComponent.candidate;
console.log(this.candidate);
}
ngOnInit() {
}
Do you have any insights on why this might be happening?