I am confused with my codes in Angular 2. In my ts
file I have:
import { Test } from '../../../../models/test';
import { TestService } from '../../../../services/test.service';
import { Job} from '../../../../models/job';
import { JobService } from '../../../../services/job.service';
export class TestTakeMcComponent implements OnInit {
company_name: string;
test: Test;
job: Job;
constructor(
private testService: TestService,
private jobService: JobService
) { }
ngOnInit() {
this.getTest();
this.getJob(this.test.id);
}
getTest(){
this.testService.getById(40).subscribe(
test => {
if(test.data.data.length != 0){
this.test = test.data.data[0];
}
}
);
}
getJob(id: number){
this.jobService.getJobByTestId(id).subscribe();
}
}
And in my HTML file I have:
<h3 class="box-title">{{ test?.title }} </h3>
Surely, the data binding {{ test?.title }}
is working and showing the data. But during the call of another function getJob(this.test.id)
on my ts
file, it says an undefined parameter.
How this became undefined when it is showing perfectly in the view? I wanted to use the data inside this.test
variable to other functions but I cannot since it is undefined.
Please someone has the same issue with me and how did you fix this problem. Thank you.