0

I have around 6 functions in my code that are not interacting with anything in the DOM, and I need those only in one specific scope.

So, is there any issue if I turn this

$scope.verifyPlaceBetAvailable = function(param) {
  //something happens here
}

into this

var verifyPlaceBetAvailable = function(param) {
  //something happens here
}

?

I mean, that is going to have any impact in the performance of my app?

Non
  • 8,409
  • 20
  • 71
  • 123
  • If you do this, then the scope will not have that function and if its been used in any view then the view will break. Performance wise $scpe should not be populated, if you are not using that function in the view then its better to remove the function from the scope. – Abhishek Prakash Jul 23 '15 at 18:34
  • if they are not you don't want to expose the function in scope otherwise don't add it to scope..I don't think so they are making any difference performance wise – Pankaj Parkar Jul 23 '15 at 18:44

5 Answers5

2

I think if you don't populate the $scope with function that will not have affect on it or on the DOM should be better....

icchio
  • 109
  • 2
  • 13
2

First of all, you should put all helpers or methods into a service, such as factory, service or provider.

About performance, I think the issue is not what you're asking. I believe can impact more about how they're written and what kind of data they're processing.

For 6 simple methods it shouldn't affect to the performance.

Nestor Britez
  • 1,218
  • 10
  • 14
2

You don't have to put every method in controller in scope.

As long as the method is not used in view or not interacting with anything in the DOM than you can remove that function from scope. There is no major performance issue.

For utility methods you should use factory,service or provider in angular js.

Refer AngularJS: Service vs provider vs factory

Darshan Patel
  • 2,839
  • 2
  • 25
  • 38
2

You should add functions in $scope only if you need them in a template otherwise use local variables or injected services if it has some business logic and does not require $scope. And it is rather improves app performance than decreases it.

Eugene Gluhotorenko
  • 3,094
  • 2
  • 33
  • 52
1

You only need to put a function in the scope if you are calling it from a view. Any other type of function could be a simple var in the controller, however many of these functions should probably be in services or factories, and not directly implemented in the controller anyway.

Daniel Cottone
  • 4,257
  • 24
  • 39