lundi 1 août 2016

dynamic ng true value in directive and binding to single controller variable

I want to make checkbox act like radio buttons i.e. only one selected at a time. I followed the following link: AngularJS: single select between multiple checkbox

I have made a directive like below:

angular
    .module('app')
    .directive('ruleEnabledCheckBoxList', ruleEnabledCheckBoxList);

    function ruleEnabledCheckBoxList() {

           return {
                  restrict: 'EA',
                  templateUrl: 'rule-checkboxlist.html',
                  scope: {
                        fieldLabel : "@",
                        valueComing : '@',
                        selectedValue:'='
                  }

           };
    }; 

The template looks like this:

 <input type="checkbox" data-ng-checked="selectedValue==valueComing"
        data-ng-true-value="valueComing"  data-ng-model="selectedValue">

This is the html:

<body ng-app="app" ng-controller="appc as appCtrl">

  <rule-enabled-check-box-list selected-value = "appCtrl.selected" field-label="Local Alarm" value-coming="Local"></rule-enabled-check-box-list>
  <rule-enabled-check-box-list selected-value = "appCtrl.selected" field-label="Central Alarm" value-coming="Central"></rule-enabled-check-box-list>

</body>

My limitation is that I want to bind both the checkboxes to the same attribute in the controller i.e. whichever is selected. I found in this link that it is not possible to give dynamic values for ng true value. AngularJS checkbox dynamic ng-true-value expression

Then is there any alternative for the above situation ?




Aucun commentaire:

Enregistrer un commentaire