仅在选择任何单选按钮时验证表单

时间:2014-10-23 16:07:15

标签: angularjs

我只想在选中任何复选框时启用我的表单的确定按钮,但不确定为什么它不起作用。这是我的代码

<form id="myForm" class="form-horizontal" role="form" name="myForm" novalidate>
    <li ng-repeat="i in templates">
        <input name="myTemplate" ng-model="myTemplate" type="radio" ng-value="{{i.id}}"  />
        <span>{{i.name}}</span>
    </li>
    <button ng-disabled="!templates.length || !myTemplate" ng-class="{'disabled' : !templates.length || !myTemplate}" class="btn btn-primary ok-btn">
        Submit
    </button>   
</form>

无论是否检查任何单选按钮

,始终禁用“提交”按钮

1 个答案:

答案 0 :(得分:2)

您需要使用Object而不是ng-model中的值。

JS:

  $scope.myTemplate = {};;

  $scope.templates = [
    {id: 1, name: 'One'},
    {id: 2, name: 'Two'},
    {id: 3, name: 'Three'}
  ];

HTML:

<form id="myForm" class="form-horizontal" role="form" name="myForm" novalidate>
    <li ng-repeat="i in templates">
        <input name="myTemplate" ng-model="myTemplate.value" type="radio" ng-value="{{i.id}}"  />
        <span>{{i.name}}</span>
    </li>
    <button ng-disabled="!templates.length || !myTemplate.value" ng-class="{'disabled' : !templates.length || !myTemplate}" class="btn btn-primary ok-btn">
        Submit
    </button>   
</form>

演示:http://plnkr.co/edit/eKH2AaPIOEcplfxOsMHQ?p=preview