1

I have a ng-repeat over 2000 employees data which is displayed in rows and inside each repeating row/block, I have a function which is passing salary information to directive for some processing. I am binding function on ng-click="calcSalary(emp)". Now my question is will ng-click="calcSalary(emp)" will be created 2000 times? And 2000 different reference will be stored at backend? Will 2000 calcSalary() functions will be created attached to scope? How will be the performance? Is there any way we write only ng-click="calcSalary(emp)" only ones and pass the employee data based on the particular row clicked?

  • calcSalary is just one function in your controller so in your front-end it is just a reference. It is created and instanciated once – Theo Apr 13 '17 at 12:12
  • 1
    If you have a ng-click inside a loop, of course the element will be created n times and the function call will be n times. This is the way any element inside a loop will work. I dont think you are creating the function n times, this is normal behavior. If you want to change any it all depends on your requirement. Preferably find an alternate way to avoid these calls inside loop – CrazyMac Apr 13 '17 at 12:14
  • `calcSalary` would be defined once in your controller. It would be called every time you click on that button with parameter passed. – Sachet Gupta Apr 13 '17 at 12:16
  • To add to above points.ng-repeat creates 2000 instances.Yes it is true and for every instance an isolated scope(i.e its own scope for every row) will be created.But performance does not degrade because the generation of duplicate instances takes place at compile phase not in link phase. – bhanu.cs Apr 13 '17 at 12:24

1 Answers1

1

No it does not have any performance issue! Even though you create inside ng-repeat it will be created n times but gets instanciated once and have impact when you click only.

Read about it Angular ng-click event delegation

Community
  • 1
  • 1
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396