简体   繁体   English

AngularJS - 未选中单选按钮

[英]AngularJS - Radio button not checked

I have an ng-repeat with a bunch of radio buttons inside: 我有一个带有一堆单选按钮的ng-repeat

<div class="panel panel-default" ng-repeat="offer in vm.offerList">
    ...    
    <td ng-show="offer.edit">
        <div class="row">                        
            <input type="radio" name="before" ng-model="offer.before" value="false">
            <input type="radio" name="before" ng-model="offer.before" value="true">
       </div>
   </td>
   ...
</div>

The model offer.before has the correct value, however, when the row is shown, the radio button doesn't appear checked. 模型offer.before具有正确的值,但是,当显示该行时,单选按钮不会显示为已选中。 Why is this happening? 为什么会这样? I need the radio button to show the selected value. 我需要单选按钮来显示所选值。

This is a fiddle example of my problem: http://jsfiddle.net/danielrvt/dpoLdgjq/ 这是我的问题的一个小例子: http//jsfiddle.net/danielrvt/dpoLdgjq/

Because anything inside attribute value gets toString() like value true will be considered as 'true' & it will looks for 'true' (string) instead of true (boolean). 因为属性value内的任何东西都得到toString()因为值true将被视为'true'并且它将查找'true' (字符串)而不是true (布尔值)。

Better use ng-value instead of value attribute. 更好地使用ng-value而不是value属性。 It will treat true value as in true of type boolean only. 它会将true值视为类型为boolean的true

Additionally in your case you have to add name attribute to be unique for each radio button group each offer element radio will be considered as unique form element. 此外,在你的情况,你必须添加name属性是每个单选按钮组的每个唯一offer元素电台将被视为独特的表单元素。

Markup 标记

<div class="row">
    {{offer.before}}
    <input type="radio" name="before{{$index}}" ng-model="offer.before" ng-value="false">
    <input type="radio" name="before{{$index}}" ng-model="offer.before" ng-value="true">
</div>

Forked Fiddle 叉小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM