Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular call function in foreach

In my component I have a foreach loop that is fired when a client is selected. Inside this loop I have to call a function in the same component.

To do this I must use this.functionName() to call the function. But obviously this will not work inside the foreach loop since 'this' is no longer the component itself.

Does anyone have a solution for this?

this.clientService.selectedClient.forEach(function(client) {
    this.getIntake(); // not working
});
like image 680
CS_student Avatar asked Feb 15 '26 03:02

CS_student


1 Answers

Use arrow functions

this.clientService.selectedClient.forEach((client) => {
    this.getIntake(); // not working
});

otherwise this will not point to the local class instance

https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions

like image 96
Günter Zöchbauer Avatar answered Feb 16 '26 16:02

Günter Zöchbauer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!