简体   繁体   English

最初不检查单选按钮

[英]Radio buttons are not checked initially

I have seen a lot of similar questions but none of them could solve my problem: 我见过很多类似的问题,但是没有一个可以解决我的问题:

I have a ng-repeat to show list of items. 我有一个ng-repeat来显示项目列表。 And each of them has a div which is shown when some condition is met: ' <div ng-if="someCondition"> </div> 它们每个都有一个div,当满足某些条件时会显示该div:' <div ng-if="someCondition"> </div>

And inside that div, i have a radio button: 在该div中,我有一个单选按钮:

<input type="radio" ng-model="item.Prop" name="published" ng-value="true" ng-checked="item.Prop"> <span>published</span>
<input type="radio" ng-model="item.Prop" name="published" ng-value="false"> <span>not published</span>

While other functionalities are working, it is not directly checked when my div becomes visible. 当其他功能正常工作时,当我的div可见时,不会直接检查它。 However, if i select one of them, it changes its ng-model. 但是,如果我选择其中之一,则会更改其ng模型。

But why is that not selected at the beginning? 但是,为什么一开始就没有选择呢?

Hard to know based on the code you provided, but I'm guessing item.Prop is undefined . 基于您提供的代码很难知道,但是我猜到item.Propundefined If you provide us with more code, a fiddle or a plunker, we may be able to help more. 如果您为我们提供了更多代码,小提琴或小插曲,我们也许可以提供更多帮助。

Example fiddle demonstrating the behaviour you're experiencing: http://jsfiddle.net/HB7LU/20982/ 示例小提琴演示您所遇到的行为: http : //jsfiddle.net/HB7LU/20982/

Example fiddle with the checkbox working as you'd wish: http://jsfiddle.net/HB7LU/20983/ 示例摆弄了您想要的复选框: http : //jsfiddle.net/HB7LU/20983/

I have found the solution: 我找到了解决方案:

The problem was that checkboxes are created for each item in the ng-repeat loop. 问题在于为ng-repeat循环中的每个项目创建了复选框。 So, every checkbox has the same name and therefore none of them could be selected. 因此,每个复选框都具有相同的名称,因此无法选择它们。

To solve it, i have just given an unique property of items at the end of the names of checkboxes. 为了解决这个问题,我刚刚在复选框名称的末尾给出了项的唯一属性。

<input type="radio" ng-model="item.Prop" name="published{{item.ID}}" ng-value="true" ng-checked="item.Prop"> <span>published</span>
<input type="radio" ng-model="item.Prop" name="published{{item.ID}}" ng-value="false"> <span>not published</span>

Here is the plunker : 这是小矮人

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

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