Skip to content

Instantly share code, notes, and snippets.

@jolks
Created August 12, 2014 01:10
Show Gist options
  • Save jolks/89872189ea0992c5320e to your computer and use it in GitHub Desktop.
Save jolks/89872189ea0992c5320e to your computer and use it in GitHub Desktop.
How to handle radio inputs under ng-repeat
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Example - example-ngValue-directive-production</title>
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.0-beta.17/angular.min.js"></script>
</head>
<body ng-app="valueExample">
<script>
angular.module('valueExample', [])
.controller('ExampleController', ['$scope', function($scope) {
// must be in JSON format
// you can test by replacing test.key to
// my.favorite
$scope.test = {key: 'unicorns'};
$scope.names = ['pizza', 'unicorns', 'robots'];
$scope.my = { favorite: 'unicorns' };
}]);
</script>
<form ng-controller="ExampleController">
<h2>Which is your favorite?</h2>
<label ng-repeat="name in names">
{{name}}
<input type="radio"
ng-model="test.key"
ng-value="name"
id="{{name}}"
name="favorite">
</label>
<div>You chose {{test.key}}</div>
</form>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment