Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angularjs "Controller as" or "$scope"

I would like to know what is the main difference between "Controller as" or "$scope" syntax in angularjs.

  1. Do they have any performance impact,if yes which syntax is preferable.
  2. "Controller as" syntax surely improve the readability of the code,as Knockout.js and other JavaScript framework follows the same syntax.
  3. $scope will provide scope inheritance which sometimes give us weird behavior like

    <div ng-controller="firstController">
     ParentController: <input type="text" ng-model="parent"/>
      <div ng-controller="secondController">
        ChildController: <input type="text" ng-model="parent" />
      </div>
    </div>
    
    app.controller('ParentController', function ($scope) {
      $scope.parent = "parentScope";
    }).controller('ChildController', function ($scope) { /*empty*/ }); 
    

    a) Initially child will get the parent property and it displays 'parentScope' when we update parent child will also get updated. But if we have changed the child property now updating the parent doesn't modify child as it has got its own scope property.

    b) If i am using controller as syntax changing child also updates the parent.

like image 276
Avinash Solanki Avatar asked Jun 04 '15 10:06

Avinash Solanki


People also ask

What is the difference between $scope and this in AngularJS?

"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.

Why $scope is used in AngularJS?

$scope is a child object that is used to bind the HTML(view) & Javascript(Controller) in a webpage. It is created with the ng-app directive. It is created with the ng-controller directive. It is available globally for all the controllers, i.e, the property assigned with “$rootscope” can be used anywhere.

What are the controllers in AngularJS?

AngularJS Controllers AngularJS applications are controlled by controllers. The ng-controller directive defines the application controller. A controller is a JavaScript Object, created by a standard JavaScript object constructor.

Which is the correct syntax of creating AngularJS controller?

13) Which of the following syntax is used to create a module in AngularJS? Answer: C is the correct option. To create a module in AngularJS, we use angular. module("app", []); syntax.


1 Answers

I've written a few answers to this question in the past, and they all essentially boil down to the same thing. In Angular, you are using $scope, even when you aren't expressly referencing it.

The ControllerAs syntax allows Angular to help you to write more efficient, fault tolerant controllers. Behind the scenes, when you use ng-controller="theController as ctrl", Angular creates theController as a property on $scope and assigns it as ctrl. You now have an object property you are referencing from scope, and are automatically protected from prototype inheritance issues.

From a performance perspective, since you are still using $scope, there should be little to no performance difference. However, since your controller is now not assigning variables directly to $scope on it's own, it does not need to have $scope injected. Also, the controller can much more easily be tested in isolation, since it is now just a plain JavaScript function.

From a forward thinking perspective, it's well known by now that Angular 2.0 will not have $scope, but instead will use features of ECMAScript 6. In any previews released by the Angular team showing migrations, they first begin by refactoring the controller to eliminate $scope. If your code is designed without the use of $scope based controllers, your first step in a migration is already complete.

From the designer's perspective, the ControllerAs syntax makes it much clearer where objects are being manipulated. Having customerCtrl.Address and storeCtrl.Address makes it much easier to identify that you have an address assigned by multiple different controllers for different purposes than if both used $scope.Address. Having two different HTML elements on a page which both are bound to {{Address}} and knowing which one is which only by the controller the element is contained in is a major pain to troubleshoot.

Ultimately, unless you are trying to spin up a 10 minute demo, you really should be using ControllerAs for any serious Angular work.

like image 108
Claies Avatar answered Sep 17 '22 17:09

Claies