javascript - 为什么我的默认单选按钮没有出现在模型中?

标签 javascript angularjs angularjs-directive angularjs-ng-model

我在表单上有一个预选单选按钮。这是 html:

<input type="radio" ng-model="bankAccount.selectedAccount" value="{{account.bankAccountId}}" class="radioChanged" ng-checked="{{account.defaultAccount}}">

表单出现并且单选按钮被选中,但当我点击我的提交按钮时,模型 bankAccount.selectedAccount 只是一个空对象。如果我单击已在 View 中选中的默认单选按钮,则它会显示在模型中。我需要做什么才能使用默认单选按钮填充我的模型?

最佳答案

您需要使用 ng-value而不是 value 插值,以便该值与模型相关联。还要注意 ng-checked 不应该有插值,当你有插值时,即使绑定(bind)值为假,它也会被视为“假”字符串并且它是真实的(如果表达式是真实的,那么特殊属性“检查"将在元素上设置)。

尝试:-

 <input type="radio" ng-model="bankAccount.selectedAccount" 
         ng-value="account.bankAccountId" class="radioChanged" 
         ng-checked="account.defaultAccount">

Binds the given expression to the value of input[select] or input[radio], so that when the element is selected, the ngModel of that element is set to the bound value.


不要将 ng-checkedng-model 混合使用,ng-checked 只是根据 truthy/falsy 条件设置元素的 checked 属性。它不会更新 ngModel。相反,如果您想选择一个 radio ,则通过将其 ng-model bankAccount.selectedAccount 设置为默认帐户的值来完成。

即:-

<input type="radio" ng-model="bankAccount.selectedAccount" 
     ng-value="account.bankAccountId" class="radioChanged" />

Plnkr

关于javascript - 为什么我的默认单选按钮没有出现在模型中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26000047/

相关文章:

javascript - 使用 JS 或 jQuery 将新节点添加到本地 XML 文件

javascript - 如何像视差效果一样在鼠标滚动时更改所选 div 中的图像?

javascript - 呈现页面时不显示文本,但如果我之后从 devtools 更改布局,则会显示文本

javascript - 如何在表格中显示这个json

javascript - 无法在 angularjs 服务中获得警报

javascript - 让 angular-masonry 工作它说它没有方法 'imagesLoaded'

javascript - 诺基亚此处 map 本地化

javascript - Angular ng 重复 : "Duplicates in a repeater are not allowed"

angularjs - 是否可以设置 ng-view 来替换 : true?

javascript - ngClick 上的 Angular 父级和子级 ngHide