我在表单上有一个预选单选按钮。这是 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-checked
与 ng-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" />
关于javascript - 为什么我的默认单选按钮没有出现在模型中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26000047/