AngularJS ng-checked指令

定义和用法

ng-checked 指令用于设置复选框(checkbox)或单选按钮(radio)的 checked 属性。

如果 ng-checked 属性返回 true,复选框(checkbox)或单选按钮(radio)将会被选中。

语法

< input  type= "checkbox|radio"  ng-checked= "expression" > < /input >

type 为 checkbox 或 radio 的 <input> 元素支持。


参数值

描述
expression 如果返回 true,将会选中元素选项。
简单实例:

JS代码

var app = angular.module('myApp', []);
app.controller('validateCtrl', function ($scope) {
    $scope.sex = true;
    $scope.agree = true;
    $scope.clickOne = function () {
        $scope.sex = !$scope.sex;
        $scope.agree = !$scope.agree;
    }
});

HTMl代码

    <h3>Radio 控件测试</h3>
    <p>
        <label>
            <input type="radio" value="男" name="sex" ng-model="value1" ng-checked="sex==true" />
            男
        </label>
        <label>
            <input type="radio" value="女" name="sex" ng-model="value1" ng-checked="sex==false" />
            女
        </label>
        <button class="btn btn-default" ng-click="clickOne();">修改</button>
    </p>
    <h3>checked 控件测试</h3>
    <p>
        <div class="checkbox">
            <label>
                <input name="agree" type="checkbox" value="同意" ng-model="value2" ng-checked="agree" />
                同意协议
            </label>
        </div>
        <div class="checkbox">
            <label>
                <input name="agree" type="checkbox" value="同意2" ng-model="value2" ng-checked="agree" />
                同意协议2
            </label>
        </div>
    </p>

特别说明:

对于radio的绑定,主要指定属性==true,也就是

<input id='sex'  name="sex"  type="radio" ng-model='sex' ng-checked="sex==true">男
而不是单纯的指定属性,如果单纯指定属性不返回属性的值

<input id='sex'  name="sex"  type="radio" ng-model='sex' ng-checked="sex">男


更多参考:

AngularJS ng-checked指令

AngularJs ng-options

AngularJs checkbox绑定

AngularJs select绑定数字类型问题

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值