AngularJS Example

by ldn_tech_exec

HTML

<div ng-app="">
  <div ng-controller="buildPlan">
    <form novalidate class="simple-form">
      

        <h1>Angular price: {{plan.total}}!</h1><br/>
        
        <h4>Shirt Tier:</h4>
        <select name="unchageablename[25]" id="unchageableid_25" class=" unchangeable_class">
            <option value="">-- Please Select --</option>
            <option value="83" price="0">Tier 1 </option>
            <option value="84" price="25">Tier 2 </option>
            <option value="85" price="75">Tier 3 </option>
        </select>

        <br/><br/>
<ul>
      <li ng-repeat="item in items">
          {{item.type}}
          
          
          
          <span ng-repeat="upgrade in item.upgrades"><input type="radio" ng-model="item.upgradeSupplement" name="upgrade1" value="{{upgrade.supplement}}" ng-change="calculateUpgradeSupplement(upgrade,item)"/>{{upgrade.value}}</span>

          <br/><br/>
          <br/><br/>
      </li>
      </ul>        
        <br/><br/>
          
    </form>
    <pre>plan = {{plan | json}}</pre>
    <pre>items = {{items | json}}</pre>
  </div>
</div>

CSS

</style> <!-- Ugly Hack due to jsFiddle issue: http://goo.gl/BUfGZ --> 
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<style>
.ng-invalid { border: 1px solid red; }

JavaScript

function buildPlan($scope) {
    

    var shirtUpgrades = [
        {
        "value": "Tier 1",
        "supplement": 0},
    {
        "value": "Tier 2",
        "supplement": 25},
    {
        "value": "Tier 3",
        "supplement": 75}
    ];

    $scope.plan = {};
    $scope.items = [
        
    {
        "type": "shirts",
        "qty": 1,
        "price": 25,
        "optionSupplement": 0,
        "upgradeSupplement": 0,
        "upgrades": shirtUpgrades}
    ];


    $scope.recalculate = function() {
        $scope.plan.total = 0;
        for (var x in $scope.items) {
            $scope.plan.total += $scope.items[x].qty * ($scope.items[x].price + $scope.items[x].upgradeSupplement + $scope.items[x].optionSupplement);
        };
    }
    $scope.subtract = function(item) {
        if (item.qty > 0) {
            item.qty = item.qty - 1;
            $scope.recalculate();
        }
    }
    $scope.calculateOptionSupplement = function(index, item) {
        item.optionSupplement = 0;
        for (var x in $scope.items) {

            for (var y in $scope.items[x].options) {

                if ($scope.items[x].options[y].selected === true) {
                    $scope.items[x].optionSupplement += $scope.items[x].options[y].supplement;

                }
            }
        }
        $scope.recalculate();

    }
    $scope.calculateUpgradeSupplement = function(upgrade, item) {
        item.upgradeSupplement = upgrade.supplement;
        $scope.recalculate();
    }

    $scope.add = function(item) {
        // no upper limit
        item.qty = item.qty + 1;
        $scope.recalculate();
    }

    $scope.recalculate();
}