Débora
Débora

Reputation: 5952

Is the $scope neccessary always for a method definition in Angular

It is clear that a method should be set to scope in order to be visible or available for the view (in html) or in a directive or in any other place where the method should be accessed, so that the method can be accessed through the $scope. My question is to know whether $scope is always necessary or a good practice to use when a method is defined. For instance, following are different method declarations:

Scenario 1. $scope.myMethod = function(){};
Scenario 2. var myMethod= function(){};

if 'myMethod' is only used in one controller, is it required to set it to the $scope? What are the advantages or why scenario 1 or 2 is good ?

What if someone has declared it as $scope.myMethod = function(){} ? is it not good or an unnecessary load to the $scope ? What can be the best practice?

NB: I don't need to make any poll here, please let me know any pros and cons

Upvotes: 1

Views: 74

Answers (4)

Rashid Javed
Rashid Javed

Reputation: 62

Scope is the glue between application controller and the view. During the template linking phase the directives set up $watch expressions on the scope. The $watch allows the directives to be notified of property changes, which allows the directive to render the updated value to the DOM.

Both controllers and directives have reference to the scope, but not to each other. This arrangement isolates the controller from the directive as well as from the DOM. This is an important point since it makes the controllers view agnostic, which greatly improves the testing story of the applications.

From the documentation

Upvotes: 1

Syam Pillai
Syam Pillai

Reputation: 5217

You need $scope to define a method only if you are calling that function from your html code.

You can use this or some_names also instead of $scope.

$scope is just to mean that the function's (mehtod's) scope is inside that controller function and can be accessible from the html code that written inside the controller

If the function is calling inside the javascript (controller) only, then use normal definition

function myMethod(){}

OR

var myMethod = function(){}

Declaring a method or variable as $scope variable is only for accessing from DOM. If you are creating a new variable in $scope. It just adding that variable to the clousure of $scope as $scope: {first, seccond, third} When ever you are calling a $scope function that just returns from the closure. There is not much load to the $scope I guess

Upvotes: 1

Jeevanandan J
Jeevanandan J

Reputation: 144

Scenario 1. $scope.myMethod = function(){};
Scenario 2. var myMethod= function(){};

Scope is the glue between the controller and the view. If you really need any variable and methods for the current view, then this should be added to the scope variable. Please see the controller as property if you don't want to add the methods to scope.

Scenario 1 If you declare a method using the scope, then this will be available/accessed from the view.

Scenario 2 If you really don't need this method to be accessed from the view, then you can remove this method from the scope.

Upvotes: 1

Fma
Fma

Reputation: 362

You mainly use the first scenario for things like binding click events. If you will only call the myMethod you don't really need to define it in scope.

For example following will need the first definition:

<button ng-click="myMethod()">My Button</button>

But following can use the second:

angular.module('myCtrl', [])
.controller('myController', function($scope) {
  var myMethod = function (text) {alert(text)};
  $scope.mySecondMethod = function () { myMethod('second'); }
  $scope.myThirdMethod = function () { myMethod('third'); }

In second case you can use mySecondMethod and myThirdMethod in event binding.

Upvotes: 1

Related Questions