Search code examples
javascriptangularjsradio-buttonangular-directiveangular-components

Value not binding in angular custom radio button component


I have created a custom radio button component in angular js which will be used across my application. following is the code i wrote for the component.

JS

angular.module("customComponent")
    .component("ngRadiobutton", {
        template:        
             '<label class="ng-control-radio-button">' +
             '  <span data-ng-bind="$ctrl.label"></span>' +
             '  <input type="radio" name="{{$ctrl.group}}" data-ng-model="$ctrl.checked"/>' +
             '  <div class="ng-control-indicator-radio"></div>' +
             '</label>' +
             '',       

        bindings: {
            label: '=?',
            checked: '=',
            group: '@'
        },
        controller: function () {
            var $ctrl = this;
            console.log($ctrl.checked);  // Data is binding properly at this stage
        }
    });

HTML

    <div data-ng-repeat="radio in vm.radioValues">
         <ng-radiobutton label="radio.label " group="group1 " checked="radio.checked"></ng-radiobutton>
    </div>

JSON

    vm.radioValues = [{ label: 'Value1', checked: true },
        { label: 'Value2', checked:false }
    ];

The issue i am facing is that the true and false value which i am setting is not getting bind with the component. by default both the radio button is unchecked. Can anyone tell me what is wrong with my code.

Thanks in advance


Solution

  • It seems like you try to have the Checkbox behavior for Radio button. (It is not really a binding problem)

    Here is a valid version of what you can have :

    angular.module("customComponent", [])
        .component("ngRadiobutton", {
            template:        
                 '<label class="ng-control-radio-button">' +
                 '  <span data-ng-bind="$ctrl.label"></span>' +
                 '  <input type="radio" name="{{$ctrl.group}}" data-ng-model="$ctrl.checked" value="{{$ctrl.label}}" />' +
                 '  <div class="ng-control-indicator-radio"></div>' +
                 '</label>' +
                 '',       
    
            bindings: {
                label: '=?',
                checked: '=',
                group: '@'
            },
            controller: function () {
                var $ctrl = this;
                console.log($ctrl.checked);  // Data is binding properly at this stage
            }
        });
    
    angular.module("customComponent").controller('Ctrl', function($scope) {
      $scope.group = {value: 'Value2' };
      
      $scope.radioValues = [
          { label: 'Value1' },
          { label: 'Value2' }
        ];
      });
    <script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script>
    
    <div ng-app="customComponent">
      <div ng-controller="Ctrl">
        <div data-ng-repeat="radio in radioValues">
             <ng-radiobutton label="radio.label" group="group1" checked="group.value"></ng-radiobutton>
        </div>
      </div>
     </div>