I would like to know what is the main difference between "Controller as" or "$scope" syntax in angularjs.
- Do they have any performance impact,if yes which syntax is preferable.
- "Controller as" syntax surely improve the readability of the code,as Knockout.js and other JavaScript framework follows the same syntax.
$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.