Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When to use $scope directly?

I just started out with Angular and have been reading through a lot of Tutorials. Now the free one at CodeSchool which was my starting point doesn't mention $scope at all.

From what I've gathered, the controllerAs syntax is relatively new (1.2.0) but it seems to allow you to get away without using $scope directly.

Some articles say "use controllerAs" with an explanation, but most just use $scope. But I couldn't find any explanation why they'd choose it.

Is this now mainly a case of favoring one over the other or are there still reasons to use $scope?

Even many new directive plugins use it instead of allowing you to bind it to a particular controller.

edit: To clarify, I want to know when to use $scope, not the reasons not to use it :)

like image 453
CWagner Avatar asked Jun 20 '14 15:06

CWagner


People also ask

What is $scope vs this?

"How does this and $scope work in AngularJS controllers?" When the controller constructor function is called, this is the controller. When a function defined on a $scope object is called, this is the "scope in effect when the function was called". This may (or may not!) be the $scope that the function is defined on.

What is @? In angular directive scope?

These prefixes are used to bind the parent scope's methods and properties to the directive scope. There are 3 types of prefixes in AngularJS: '@' – Text binding / one-way binding. '=' – Direct model binding / two-way binding.

Why we use scope apply ()?

The reason why we have to call $scope. $apply() when we update values in our event listener is because Angular doesn't know that we've updated the scope.

What is scope apply?

$scope. $apply() takes a function or an Angular expression string, and executes it, then calls $scope. $digest() to update any bindings or watchers.


1 Answers

In the Angular documentation for ngController it explains the advantages to using 'controller as' vs. injecting $scope. Here's what it says:

  • Using controller as makes it obvious which controller you are accessing in the template when multiple controllers apply to an element.
  • If you are writing your controllers as classes you have easier access to the properties and methods, which will appear on the scope, from inside the controller code.
  • Since there is always a . in the bindings, you don't have to worry about prototypal inheritance masking primitives.

For my own part I've found using 'controller as' quite beneficial as it forces me to consider whether code I'm adding to a controller would be more appropriately added into a service or directive.

For example: watches. Watches are something you should avoid in controllers but having easy access to $scope allows you to set them up easily. Using 'controller as' has forced me to think more carefully about whether I really need to do a watch. Usually a watch can be accomplished with a directive. This has led me to create smaller controllers that only set up an initial state and communicate with services, a pattern I've found much more performant and maintainable.

like image 135
JPRO Avatar answered Oct 07 '22 17:10

JPRO