AngularJS-指令templateUrl中的表单验证

问题描述 投票:3回答:1

我正在尝试从templateUrl内部使用angular的形式验证。

我有一个加载templateUrl的指令,其中我有一个带有输入的表格,这些输入从指令范围中获取ng-required和ng-regex值。现在,我尝试放入指令的范围形式:“ =”,但是当我访问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;
            }
        }
javascript angularjs scope directive
1个回答
1
投票

您在这里:

Working Form

  • 表单按钮在表单外部,也可以在指令外部。 Angularjs没关系。

  • 有两个输入,都需要,并且都具有如您所述的正则表达式验证。

  • 有一个带有templateURL的指令

这里要记住的重要一点是,表单必须具有名称,然后由该名称引用,例如:scope.myForm when

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

但是,在所有输入均有效之前,实际的表单才有效,因此,您可能只需要像提供的示例中那样在表单本身上调用有效。

如果将按钮移至指令之外,则必须将Submit函数从指令移至控制器(最有可能)。

让我知道这是否有帮助,我可以根据需要进行更改。另外,首先尝试将问题与问题混为一谈,然后再发布新代码的内容;只需拨叉车并提供链接。

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

<!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="[email protected]" data-semver="1.3.7" src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.7/angular.js"></script>
    <script data-require="[email protected]" 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
});
© www.soinside.com 2019 - 2024. All rights reserved.