Angular: radio buttons 14530785

http://angularjs.org/

by mrajcok

HTML

<div ng-controller="MyCtrl">
    <table>
        <tr ng-repeat="location in merchant.storeLocations">
            <td>{{location.name}}</td>
            <td>
                <input type="radio" ng-model="$parent.storeDefault" value="{{location.id}}" name="isDefault_group">
            </td>
        </tr>
    </table>
    <button ng-click="save()">save</button>
        <br>{{storeDefault}}
        <br>{{merchant.storeLocations}}
</div>

JavaScript

var myApp = angular.module('myApp', []);

function MyCtrl($scope) {
    $scope.merchant = { storeLocations: [
       { name: 'Target', id: 1, isDefault: false },
       { name: 'WalMart',id: 2, isDefault: false },
       { name: 'BestBuy',id: 3, isDefault: true  }
    ]};
    for (var i = 0; i < $scope.merchant.storeLocations.length; i++) {
        if ($scope.merchant.storeLocations[i].isDefault) {
            $scope.storeDefault = $scope.merchant.storeLocations[i].id
            break;
        }
    };
    $scope.save = function () {
        for (var i = 0; i < $scope.merchant.storeLocations.length; i++) {
            if ($scope.merchant.storeLocations[i].id == $scope.storeDefault) {
                $scope.merchant.storeLocations[i].isDefault = true
            } else {
                $scope.merchant.storeLocations[i].isDefault = false
            }
        }
        // do save here
    }
}