radio button id selected for last element only in angular 6 code example

Example: Angular - dynamically set model property of object bound to radio button based on its state

function setTrueFalseRadioState(answer) {
     angular.forEach(vm.files, function (a) {
        if (a.IsMyChoice !== answer.IsMyChoice) {
            a.IsMyChoice = false;
        }
     });
 };

 vm.setTrueFalseRadioState = setTrueFalseRadioState;


I discovered that if you want to send the object data to the backend server, you need to set the ng-value to its index
  
  <input type="radio"
         id="isChoice_{{$index + 1}}"
         name="response"
         ng-value="{{$index + 1}}"
         ng-required="!file.IsMyChoice"
         ng-change="get.setTrueFalseRadioState(file)"
         data-ng-model="file.IsMyChoice" />

and the use javascript to check the values like:

  if (file.IsMyChoice !== undefined) {
     file.IsMyChoice !== false ?
     file.IsMyChoice = 'true' : file.IsMyChoice = 'false';
  }

Tags:

Misc Example