【问题标题】:Validating checkboxes angular验证复选框角度
【发布时间】:2016-08-11 08:30:17
【问题描述】:

如何验证此复选框,必须至少选中一个,如果没有,则必须有一些消息或警报

<div class="form-group" ng-class="{ 'has-error' : actionsAddForm.active.$invalid && !actionsAddForm.active.$pristine }">
    <label class="control-label col-sm-2">Days*</label>
    <div class="col-sm-10">
        <label>Monday
          <input type="checkbox" ng-model="actions.value1">
        </label>
        <label>Tuesday
          <input type="checkbox" ng-model="actions.value2">
        </label>
        <label>Wednesday
          <input type="checkbox" ng-model="actions.value3">
        </label>
        <label>Thursday
          <input type="checkbox" ng-model="actions.value4">
        </label>
        <label>Friday
          <input type="checkbox" ng-model="actions.value5">
        </label>
        <label>Saturday
          <input type="checkbox" ng-model="actions.value6">
        </label>
        <label>Sunday
          <input type="checkbox" ng-model="actions.value7">
        </label>
    </div>

【问题讨论】:

    标签: angularjs validation checkbox


    【解决方案1】:

    这是可以做到的:

    <div ng-hide="actions.value1 || actions.value2 || actions.value3...">
        You have an error 
    </div>
    

    否则,如果您希望能够使用 $valid,... 您将必须创建一个继承自 ngModelController 的自定义指令,并在其中添加您的自定义验证器:https://docs.angularjs.org/api/ng/type/ngModel.NgModelController

    【讨论】:

    • 好的,这是最基本和最有效的解决方案,谢谢,现在就这样吧,开始使用自定义指令
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-03
    • 1970-01-01
    • 2010-10-05
    • 2015-04-02
    • 2015-05-24
    • 1970-01-01
    相关资源
    最近更新 更多