I realize that AngularJS already has an input[radio] directive and I want to leverage that as much as possible.
I created a JSFiddle here, but I can't figure out how to get the ng-model property to work properly. I'm selecting each radio, but the selectedValue doesn't change.
Also, please tell me anything that I'm doing wrong here. I'm sure I could make some other improvements.
The HTML:
<div data-ng-controller="controller">
<div
data-ng-repeat="radio in radios"
data-ng-model="selectedValue"
data-name="radio1"
data-label="{{radio.label}}"
data-value="{{radio.value}}"
data-labeled-radio></div>
<br>
selected value: {{selectedValue}}
</div>
The JavaScript:
angular.module('app', [])
.controller('controller', function($scope) {
$scope.selectedValue = 'FOO';
$scope.radios = [
{ label: 'foo', value: 'FOO' },
{ label: 'bar', value: 'BAR' }
];
})
.directive('labeledRadio', function(){
return {
require: ['ngModel', 'value'],
restrict: 'A',
replace: true,
template: [
'<label class="radio">',
' <input class="radio__input" type="radio" data-ng-model="ngModel" name="{{name}}" value="{{value}}">',
' <span class="radio__label">{{label}}</span>',
'</label>'
].join(''),
scope: {
ngModel: '=',
label: '@',
name: '@',
value: '@'
}
}
});