AngularJS: Protoypical Inheritance

by goldfingerxyz

HTML

<script src="http://ci.angularjs.org/job/angular.js-angular-master/ws/build/angular.js"></script>
<div ng-controller="Parent">
  A = {{data.a}}<br>
  B = {{b}}
<div ng-controller="Child">
    <a ng-click="incrementA()">inc A</a> | <a ng-click="incrementB()">inc B</a>
</div>
</div>

JavaScript

function Parent($scope) {
    $scope.data = {
        a: 0
    };
    $scope.b = 0;
}

function Child($scope) {
    $scope.incrementA = function() {
        // it's through "data" obj, so we are chaning the value defined on parent scope
        $scope.data.a++;
    };
    
    $scope.incrementB = function() {
        // $scope.b++ would create "b" property on current scope
        $scope.$parent.b++;
    };
}