0

I need to know the difference between functions and variables defined without using $scope (e.g. $scope.myfunction = function(){}).

I need to know if I define a variable or a function inside a controller without making it a property of the $scope, what will be the scope of these variables/methods? will it get the $windows scope?

If I want to define some functions inside my controller to organize my code, is it ok to define these functions without a $scope method?

what is the best practice?

nzhmz
  • 259
  • 1
  • 2
  • 9

2 Answers2

0

Yes. In general don't pollute $scope with functions that won't be needed in the views.

Adam
  • 2,204
  • 2
  • 15
  • 15
0

The function scoping of javascript applies all the same. There is nothing inherently special about angular in this regard. You can read about it here: What is the scope of variables in JavaScript?

Then you will know that functions declared inside a controller (or function) will not have window scope. It is OK to define functions that are not properties of a $scope object. In fact, you should only attach variables/functions to a $scope object if you intend on using it within a template (best practice).

Community
  • 1
  • 1
sahbeewah
  • 2,690
  • 1
  • 12
  • 18