Rostyslav Kulchytskyi Rostyslav Kulchytskyi - 1 month ago 8
AngularJS Question

How to get value of radio button outside of the ng-repeat scope?

I'm trying to get value of the radio button when the button is checked.
As you can see it works fine in the first of ng-repeat scope

<div ng-controller="myController as vm">
<ul>
<li ng-repeat="c in vm.questionbox">
<label for="ques{{$index}}"><input ng-model="$parent.mode" type="radio" name="question" id="ques{{$index}}" ng-value="{{c.value}}"> {{c.name}}</label>
<ul ng-show="mode == c.value">
<li ng-repeat="(key, value) in c.answers">
<label for="ans{{c.id}}{{$index+1}}"><input ng-model="$parent.result.value" value="{{value}}" type="radio" name="answer{{c.id}}" id="ans{{c.id}}{{$index+1}}"> {{value}}</label>
</li>
</ul>
<div>currently selected: {{result.value}}</div>
</li>
</ul>
<div>currently selected: {{result.value}}</div>
</div>


But I'm also want to get this value after ul tag (outside ng-repeat="c in vm.questionbox"). Please some solutions.

You can see full code at this link: http://plnkr.co/edit/Ovwh4M2COIuqDXaQVU5X?p=preview

Answer

Save the result of each radiobutton in the list.

<li ng-repeat="(key, value) in c.answers">
    <label for="ans{{c.id}}{{$index+1}}"><input ng-model="c.selectedAnswer" value="{{value}}" type="radio" name="answer{{c.id}}" id="ans{{c.id}}{{$index+1}}"> {{value}}</label>
</li>

Print the selected results with a simple ng-repeat

<div>currently selected: <div ng-repeat="answer in vm.questionbox">{{answer.selectedAnswer}} </div></div>

http://plnkr.co/edit/MCFszBx9fJy6ALCtKrvK?p=preview

Comments