admin管理员组文章数量:1389783
In my ponent 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 ponent.
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 ponent itself.
Does anyone have a solution for this?
this.clientService.selectedClient.forEach(function(client) {
this.getIntake(); // not working
});
In my ponent 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 ponent.
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 ponent itself.
Does anyone have a solution for this?
this.clientService.selectedClient.forEach(function(client) {
this.getIntake(); // not working
});
Share
Improve this question
asked Dec 30, 2017 at 12:10
CS_studentCS_student
1074 silver badges9 bronze badges
1 Answer
Reset to default 8Use arrow functions
this.clientService.selectedClient.forEach((client) => {
this.getIntake(); // not working
});
otherwise this
will not point to the local class instance
https://developer.mozilla/en-US/docs/Web/JavaScript/Reference/Functions/Arrow_functions
本文标签: javascriptAngular call function in foreachStack Overflow
版权声明:本文标题:javascript - Angular call function in foreach - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744676661a2619153.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论