gpt4 book ai didi

javascript - 未选中嵌套 ng-repeat 中的 Angularjs 单选按钮

转载 作者:行者123 更新时间:2023-11-29 21:51:33 25 4
gpt4 key购买 nike

我有一个 html 模板,其中一个 ng-repeat 嵌套在父 ng-repeat 中。父 ng-repeat 包含单选按钮,用户可以在其中选择分别对应于值 1 和 0 的“满意”或“不满意”。如果用户选择不满意,则会显示详细的选项列表,以便他们可以选择更多 radio 以获取更多信息。这是 html;

<div class="group" ng-repeat="group in Groups">
<table>
<tr>
<td>{{group.Description}}</td>
<td><input type="radio" value="1" name="{{group.Id}}" ng-model="group.SatisfactionLevel" /></td>
<td><input type="radio" value="0" name="{{group.Id}}" ng-model="group.SatisfactionLevel" /></td>
</tr>
</table>
<div class="group-detail" ng-class="{'hidden': group.SatisfactionLevel != 1}">
<table>
<tr ng-repeat="item in group.Details">
<td>{{item.Description}}</td>
<td><input type="radio" value="1" name="{{item.Id}}" ng-model="item.SatisfactionLevel" /></td>
<td><input type="radio" value="0" name="{{item.Id}}" ng-model="item.SatisfactionLevel" /></td>
</tr>
</table>
</div>

服务器返回的json是这样的;

"Groups": [
{
"Id":"AA",
"Description":"Service",
"SatisfactionLevel":1,
"Details":[{"Id":"AA1","Description":"Cleanliness","SatisfactionLevel":1},
{"Id":"AA2","Description":"Timeliness","SatisfactionLevel":1}
]
},
{
"Id":"AB",
"Description":"Food",
"SatisfactionLevel":1,
"Details":[{"Id":"AB1","Description":"Price","SatisfactionLevel":1},
{"Id":"AB2","Description":"Portion","SatisfactionLevel":1}
]
}
]

除了未选中嵌套的 ng-repeat 中的单选按钮外,一切正常。我可以在 fiddler 中看到 Satisfactionlevel 属性包含值。有人看到我哪里出错了吗?谢谢

更新

代码确实没有错。事实证明,Groups 中的不同项目可以包含相同的 Details 项目。由于我使用 name="{{item.Id}}" 作为 name 属性,其他组详细信息中的其他 radio 具有相同的名称但不同的值导致以前具有相同名称的 radio 不受检查。这是我的解决办法;

<td><input type="radio" value="1" name="{{group.Id}}-{{item.Id}}" ng-model="item.SatisfactionLevel" /></td>

因为组 ID 是唯一的。

最佳答案

首先,您在两个 input 中具有相同的值 - 我猜这是一个拼写错误?

其次,如果你使用value="1",它被解释为一个字符串"1",但是你的模型是一个整数1.

相反,使用 ng-value:

<input type="radio" ng-value="0" name="{{item.Id}}" ng-model="item.SatisfactionLevel">
<input type="radio" ng-value="1" name="{{item.Id}}" ng-model="item.SatisfactionLevel">

关于javascript - 未选中嵌套 ng-repeat 中的 Angularjs 单选按钮,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28889155/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com