Using Angular ngModel with calculated variables

by Tarek Faham

HTML

<div ng-app>
    <div ng-controller="CTRL">
        <input type="text" ng-model="val1" />
        <input type="text" ng-model="val2" />
        <input type="text" ng-model ="theTotal" ng-value="total()" />
        <p>{{'Angular works!'}}</p>
    </div>
</div>

JavaScript

function CTRL ($scope) {
    $scope.val1 = 3;
    $scope.val2 = 4;
    $scope.total = function() {return $scope.theTotal = $scope.val1 + $scope.val2;};
}