AngularJs:如何设置基于模型检查的单选按钮 [英] AngularJs: How to set radio button checked based on model

查看:14
本文介绍了AngularJs:如何设置基于模型检查的单选按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在 storeLocations 对象中返回了一个具有 isDefault 值的模型.如果 isDefault 返回 true,我不想将组中的单选按钮设置为选中状态.

I have a model returning in the storeLocations object with a isDefault value. if isDefault returns true, I wan't to set that radio button in the group as checked.

不确定我是否需要执行 $each(data, function(index,value) 并遍历返回的每个对象,或者是否有使用角度构造更简单的方法.

Not sure if I need to do a $each(data, function(index,value) and iterate through each object returned or if there's an easier way to do this using angular constructs.

对象:

storeLocations = [
 {
  ... more values,
  isDefault: true
 }
]

标记:

    <tr ng-repeat="location in merchant.storeLocations">
        <td>{{location.name}}</td>
        <td>{{location.address.address1}}</td>
        <td>{{location.address.address2}}</td>
        <td>{{location.address.city}}</td>
        <td>{{location.address.stateProvince}}</td>
        <td>{{location.address.postalCode}}</td>
        <td>{{location.address.country}}</td>
        <td>{{location.website}}</td>
        <td>{{location.zone}}</td>
        <td><input type="radio" ng-model="location.isDefault" value="{{location.isDefault}}" name="isDefault_group"></td>

推荐答案

使用 ng-value 而不是 value.

ng-value="true"

带有 ng-checked 的版本更糟糕,因为代码重复.

Version with ng-checked is worse because of the code duplication.

这篇关于AngularJs:如何设置基于模型检查的单选按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆