javascript - Angularjs "Controller as" or "$scope" -


i know main difference between "controller as" or "$scope" syntax in angularjs.

  1. do have performance impact,if yes syntax preferable.
  2. "controller as" syntax surely improve readability of code,as knockout.js , other javascript framework follows same syntax.
  3. $scope provide scope inheritance give 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) child parent property , displays 'parentscope' when update parent child updated. if have changed child property updating parent doesn't modify child has got own scope property.

    b) if using controller syntax changing child updates parent.

i've written few answers question in past, , boil down same thing. in angular, using $scope, when aren't expressly referencing it.

the controlleras syntax allows angular write more efficient, fault tolerant controllers. behind scenes, when use ng-controller="thecontroller ctrl", angular creates thecontroller property on $scope , assigns ctrl. have object property referencing scope, , automatically protected prototype inheritance issues.

from performance perspective, since still using $scope, there should little no performance difference. however, since controller not assigning variables directly $scope on it's own, not need have $scope injected. also, controller can more tested in isolation, since plain javascript function.

from forward thinking perspective, it's known angular 2.0 not have $scope, instead use features of ecmascript 6. in previews released angular team showing migrations, first begin refactoring controller eliminate $scope. if code designed without use of $scope based controllers, first step in migration complete.

from designer's perspective, controlleras syntax makes clearer objects being manipulated. having customerctrl.address , storectrl.address makes easier identify have address assigned multiple different controllers different purposes if both used $scope.address. having 2 different html elements on page both bound {{address}} , knowing 1 controller element contained in major pain troubleshoot.

ultimately, unless trying spin 10 minute demo, should using controlleras serious angular work.


Comments

Popular posts from this blog

Magento/PHP - Get phones on all members in a customer group -

php - .htaccess mod_rewrite for dynamic url which has domain names -

Website Login Issue developed in magento -