Imagine a scenario where the need arises to use a conditional statement that performs the same action but calls a different method.
const c = true; // just for illustration
let b = '';
if (c) {
b = 'method1';
} else {
b = 'method2';
}
function a(){
this.service.b(filter).subscribe((result) =>{
// various actions take place
});
}
Can we utilize the variable b
dynamically to invoke a method from the service?
Check out the Stackblitz example
Upon examining the provided example, an updated snippet can be implemented as follows:
if (this.title == 'Students') {
this.studentService.getStudents().then(results=> this.lists = results);
} else {
this.studentService.getCandidates().then(results => this.lists = results);
}
However, is there a way to achieve something like this instead:
this.studentService.get[this.title]().then(results=> this.lists = results);
It was mentioned that it may not work in such manner as described above.