mardi 19 mai 2015

Add a number if checkbox is checked subtract if its unchecked in angularjs

Currently I have a checkbox that will trigger my function every time it is clicked. How can I make it add the number if its checked and subtract the number when its unchecked?

I have found several related issues but can't quite find what I am looking for. JSFIDDLE: http://ift.tt/1JY4BrX

HTML:

<div ng-app>
    <div ng-controller="showCrtl">            
             <input type="checkbox" name="additionalCoverage" ng-click="cost(150)"> <label>Add this coverage $150/YR</label><br>
             <input type="checkbox" name="additionalCoverage" ng-click="cost(40)"> <label>and or this coverage $40/YR</label>

        <h1 class="annualCost">TOTAL ANNUAL COST<br>OF HOME WARRANTY</h1>
        <span style="color: green; font-size: 45px; line-height: 2;">{{annualCost}}</span><br>
                 </div>
</div>

JS:

function showCrtl($scope){
    $scope.dollarAmmount = 0.00;
    $scope.annualCost = "$" + $scope.dollarAmmount + ".00";

    $scope.cost = function(amt) {
        $scope.dollarAmmount = $scope.dollarAmmount + amt;
        $scope.annualCost = "$" + $scope.dollarAmmount + ".00";
        console.log($scope.dollarAmmount)
    };
}




Aucun commentaire:

Enregistrer un commentaire