【问题标题】:Anguarjs Directive at Form Level, accessing all fields and checking for validation表单级别的 Angularjs 指令,访问所有字段并检查验证
【发布时间】:2015-03-21 19:24:11
【问题描述】:

在一个表单 (itemSelectionForm) 中,我使用 ng-repeat 呈现多个表。在每个表中,我都有单选按钮,其名称后附有索引。现在,我想编写一个 Angular JS 指令(selectAtleastOneItemToReturn),我将把它放在表单上(使用 select-atleast-one-item),它将基于子表单选按钮进行表单验证。现在,我不知道如何在该指令中访问这些子表单选按钮及其值,以便编写验证代码。而且,如果单选按钮的值发生变化,我想一次又一次地进行验证。如果表单无效,则下一步按钮将被禁用。 HTML如下。

<div class="panel-group" data-ng-form="itemSelectionForm" select-atleast-one-item>
  <div class="panel panel-default" data-ng-repeat="item in items">
    <div class="panel">
      <div class="panel-body">       
          <table class="table table-bordered">
            <thead>
              <tr>
                <th>Action</th>
                <th>Item Description</th>
              </tr>
            </thead>
            <tbody>
              <tr>
                <td>
                  <label>
                  <input type="radio" name="inputRadios{{$index}}" value="option 1"  data-ng-model="item.action" required/>
                  </label>
                </td>
                <td>{{item.description}}</td>
              </tr>
              <tr>
                <td>
                  <label>
                  <input type="radio" name="inputRadios{{$index}}" value="option 2" data-ng-model="item.action" required/>
                  </label>
                </td>
                <td colspan="2">Option 2</td>
              </tr>
              <tr>
                <td>
                  <label>
                  <input type="radio" name="inputRadios{{$index}}" value="option 3" data-ng-model="item.action" required/>
                  </label>
                </td>
                <td colspan="2">Option 3</td>
              </tr>
            </tbody>
          </table>
      </div>
    </div>
  </div>
</div>

<div class="right">    
  <button type="submit" class="btn green-button left space-left" data-ng-disabled="itemSelectionForm.$invalid" data-ng-click="goForward()">Next</button>
</div>

【问题讨论】:

    标签: javascript html angularjs


    【解决方案1】:

    最简单的方法可能是将表单对象传递给指令,并在您需要的属性上放置一个$watch,在您的情况下可能是$error - 尽管我不太清楚你是什么'正在努力实现...

    JS:

    var app = angular.module('myApp', []);  
    app.controller('myCtrl', function($scope, $log){
        $scope.items = [
            {
                action: 'fooAction',
                description: 'Foo'
            },
            {
                action: 'barAction',
                description: 'Bar'
            }
        ];
    });    
    app.directive('selectAtleastOneItem', function(){
        return {
            restrict: 'A',
            scope: {
                frm: '=selectAtleastOneItem'
            },
            link: function(scope, element, attrs){
                scope.$watch('frm.$error', function(newVal, oldVal){
                    console.log(newVal);
                }, true);
            }
        }
    });
    

    HTML:

    <div ng-app="myApp">
        <div ng-controller="myCtrl">
            <div ng-form="itemSelectionForm" select-atleast-one-item="itemSelectionForm">
                <div ng-repeat="item in items">
                    {{item.description}}
                    <input type="radio" name="inputRadios{{$index}}" ng-model="item.action" required>
                </div>
        </div>
    </div>
    

    小提琴:http://jsfiddle.net/dwkmy20j/1/

    希望这能让你有所收获。

    【讨论】:

    • 谢谢.....但是我有每个项目有 3 个动作的项目......用户应该在至少一个项目上选择一个特定的动作......并且我想使用指令在表单级别进行验证.....现在在指令中我想访问所有具有动态名称的动态生成的无线电,如果条件满足,则表单将有效,否则会将表单设置为无效。 .......如果用户更改任何项目的操作......指令应该一次又一次地进行验证......我不想看 form.error
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-08-02
    • 1970-01-01
    • 2016-10-31
    • 2016-07-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多