AngularJS - 在指令的templateUrl中进行表单验证

3
我将尝试在templateUrl中使用Angular的表单验证。我有一个指令,加载了一个包含表单的templateUrl,其中的输入框从指令作用域获取ng-required和ng-regex值。现在,我尝试在指令作用域中加入form: '=', 但是当我访问scope.form时,它是未定义的。我必须指出,我的提交按钮位于表单之外,当点击ng-click='save($index)'时,它必须首先检查表单是否有效,然后继续保存编辑后的数据。scope.save()在我的指令中被定义。这是来自模板的内容:
 <tr data-ng-repeat="row in source.data "  data-ng-class="{'selected':row.$_selected}" >
            <td data-ng-repeat="c in settings.columns" data-ng-click="toggleSelect(row)" >         
                <form name="editForm" id="editForm" novalidate> 
                    <div ng-switch on="c.type" ng-show="editMode[$parent.$index]">
                        <span ng-switch-when="text" >
                            <input type="{{c.type}}"  data-ng-model="row[c.name]" ng-required="{{c.isRequired}}" ng-pattern="{{c.regex}}"/>
                        </span>
                        <span ng-switch-when="select" >
                            <select  data-ng-model="row[c.name]" ng-selected="row[c.name]" ng-init="row[c.name]" ng-options="item.value as item.name for  item in c.items" ng-required="{{c.isRequired}}">
                                <!--<option data-ng-repeat="(value, name) in c.items" value="{{value}}">{{name}}</option>-->
                            </select>
                        </span>
                        <span ng-switch-when="textarea">
                            <textarea  ng-model="row[c.name]" ng-required="{{c.isRequired}}" ng-pattern="{{c.regex}}">                                                      
                            </textarea>
                        </span>
                        <span ng-switch-when="checkbox">
                            <!--<label for="checkboxInput">{{c.name}}</label>-->
                            <input name="checkboxInput" type="checkbox" data-ng-model="row[c.name]" ng-true-value="{{c.true}}" ng-false-value="{{c.false}}"/>
                        </span>
                        <span ng-switch-default="">
                            {{row[c.name]}}
                        </span>                     
                    </div>          
                </form>         
                <span ng-hide='editMode[$parent.$index]'>{{row[c.name]}}</span>
            </td>                  
            <td>
                <a href="{{row[settings.specialFields.url]}}" class="btn btn-default opacity75" data-ng-if="row[settings.specialFields.url]">
                    <span class="glyphicon glyphicon-chevron-right"></span>
                </a>                                      
            </td>                
            <td data-ng-if="row[settings.specialFields.isEditable]">
                <button ng-click="edit(row)" ng-show="!editMode[$index]" class="btn btn-primary" >
                    edit {{$index}}
                </button>                   
                <button ng-click="save($index)" ng-disabled=""  ng-show="editMode[$index]" class="btn btn-primary">
                    save
                </button>
                <button ng-click="cancel($index)" ng-show="editMode[$index]"  class="btn btn-default">
                    cancel
                </button>                     
            </td> 
        </tr>

这是来自我的指令:

scope: {
        settings: '=',
        source: '=',
        form: '='

    },

    templateUrl: function (element, attr) {
        return attr.templateUrl || 'src/grid.html';
    },
    link: function (scope, element, attrs) {

        scope.editMode = [];     

        scope.editing = false;
        scope.previousData = {};

        scope.edit = function(field) { 

            scope.editing = scope.source.data.indexOf(field);               
            scope.editMode[scope.editing] = true;               
            scope.previousData = angular.copy(field);

        };

        scope.save = function(index){
            console.log(scope.form);
            scope.editMode[index] = false;              

            if (scope.editing !== false ) {     
                //do the saving            
                scope.editing = false;
            } 
        };



        scope.cancel = function(index){
            scope.editMode[index] = false;

            if (scope.editing !== false) {
                scope.source.data[scope.editing] = scope.previousData;
                scope.editing = false;
            }
        }

你的保存功能必须放在表单外面,这是有原因的吗? - SoEzPz
1个回答

1

给你:

工作表单

  • 表单按钮在表单外部,也可以在指令外部。这与Angularjs无关。

  • 有两个输入,都需要并且都有正则表达式验证,就像你所说的那样。

  • 有一个带有templateURL的指令

重要的是要记住,表单必须有一个名称,然后通过该名称引用它,例如:scope.myForm when

你不必像我在plunker中那样命名输入字段,但如果你这样做,并且它们彼此完全不同,则可以执行以下操作:scope.myForm.myInputName.$valid以查看每个输入是否有效(如果您愿意)。

但是,实际表单将在所有输入都有效之前无效,因此您可能只需要在提供的示例中调用表单本身的valid。

如果将按钮移动到指令外部,则可能需要将提交函数从指令移动到控制器(最有可能)。

如果有帮助,请让我知道,如果需要的话我可以进行更改。此外,请先在 Plunker 上尝试您的问题,然后再发布新代码的情况;只需 fork Plunker 并提供链接即可。

这是 Plunker 代码,以防万一...

<!DOCTYPE html>
  <html>

  <head>
    <link data-require="bootstrap@*" data-semver="3.2.0" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" />
    <script data-require="jquery@*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
    <script data-require="bootstrap@*" data-semver="3.2.0" src="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/js/bootstrap.js"></script>
    <script data-require="angular.js@1.3.7" data-semver="1.3.7" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
    <script data-require="angular-ui-bootstrap@0.12.0" data-semver="0.12.0" src="http://angular-ui.github.io/bootstrap/ui-bootstrap-tpls-0.12.0.min.js"></script>
    <link rel="stylesheet" href="style.css" />
    <script src="script.js"></script>
  </head>

  <div ng-app="myApp">
    <div ng-controller="MyCtrl">
      <my-directive></my-directive>
    </div>
  </div>
</html>

<form name="myForm" novalidate>
  <label>Input #1 (required)</label><br>
  <input ng-model="form.data.myName" name='myName' ng-pattern="/\D+/" ng-required="true" /> <span ng-show="myForm.myName.$error.pattern">Takes anything but digits</span><br>
  <br>
  <label>Input #2 (required)</label><br>
  <input ng-model="form.data.myEmail" name='myEmail' ng-pattern="/\d+/" ng-required="true" /> <span ng-show="myForm.myEmail.$error.pattern">Takes only digits</span>
</form>
<br>
<p># I'm a button that is outside of the form!</p>
<p ng-model="form.submitted">Form Submitted: <span class="blue">{{ form.submitted }}</span></p>
<p>Form Valid?: <span class="blue">{{ myForm.$valid }}</span></p>
<button ng-click="submitForm('myForm')">Submit Form</button>

<p ng-model="form.data">Here is the Form data:</p>
{{ form.data }}


var app = angular.module('myApp', []);

app.controller('MyCtrl', function ($scope) {});

  app.directive("myDirective", function () {

    return {
      restrict: 'AE',
      require: '?ngModel',
      templateUrl: 'my-directive.html',

      link: function (scope, elm, attrs, ctrl) {
      scope.form = {submitted: false, data: {}};

      scope.submitForm = function(formname){
        console.log(scope[formname].myEmail)
        console.log(scope.formname)
        console.log(scope[formname].$valid)
        scope.form.submitted = true;
      }
    } // end link
  } // end return
});

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接