简体   繁体   English

使用ng-model与嵌套ng-repeat内的单选按钮不起作用

[英]using ng-model with radio buttons inside nested ng-repeat not working

Hey i'm trying to display a list of radio buttons inside an ng-repeat and it doesn't seem to be taking in the initial values of the fields i bind to ng-model on the input. 嘿我正在尝试在ng-repeat中显示单选按钮列表,它似乎没有考虑我在输入上绑定到ng-model的字段的初始值。

When i give a simple array to an ng-repeat, the buttons are displayed with the initial values properly. 当我给ng-repeat一个简单的数组时,按钮会正确显示初始值。 But if a nested ng-repeat comes into the picture, only the last item of each list initializes with a value 但是如果嵌套的ng-repeat进入图片,则只有每个列表的最后一项用值初始化

Here's my pen: 这是我的笔:

https://codepen.io/alokraop/pen/JXLZBp https://codepen.io/alokraop/pen/JXLZBp

I have no idea where i'm going wrong. 我不知道我哪里出错了。 I'm making sure that the name attribute of the radio button is unique to each group. 我确保单选按钮的name属性对每个组都是唯一的。

Appreciate the help. 感谢帮助。

The issue here is that you can't interpolate an Angular value into the attr name . 这里的问题是您不能将Angular值插入到attr name If we remove the name attr from our HTML, we get the expected result. 如果我们从HTML中删除名称attr,我们会得到预期的结果。 See Below. 见下文。

<div ng-app="sample">
  <div ng-controller="sampleController as sample">
    Single ng-repeat:
    <div ng-repeat="queue in sample.queues">{{queue.name}} status:
      <input type="radio" name="queue-{{$index}}" ng-model="queue.condition" value="hooked" /> hooked
      <input type="radio" name="queue-{{$index}}" ng-model="queue.condition" value="unhooked" /> unhooked
      <input type="radio" name="queue-{{$index}}"  ng-model="queue.condition" value="partial" /> partial
    </div>
    <br />
    Nested ng-repeat:
    <div ng-repeat="qm in sample.qms">
      {{qm.name}} queues:
      <div ng-repeat="queue in qm.queues">{{queue.name}} status:
        <input type="radio" ng-model="queue.condition" value="hooked" /> hooked
        <input type="radio" ng-model="queue.condition" value="unhooked" /> unhooked
        <input type="radio" ng-model="queue.condition" value="partial" /> partial
      </div>
    </div>
  </div>

</div>

If we want to include a name dynamically, look into an Angular name directive such as: Dynamic form name attribute <input type="text" name="{{ variable-name }}" /> in Angularjs 如果我们想要动态包含名称,请查看Angular名称指令,例如: Angularjs中的动态表单名称属性<input type =“text”name =“{{variable-name}}”/>

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

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