2014-07-18 53 views
3

我想使用角度的表單驗證從templateUrl內部的templateUrl。AngularJS - 表單驗證的指令

我有一個加載templateUrl在我所與得到指令範圍NG-需要和NG-正則表達式的值輸入的形式的指令。現在,我試圖把我的指令的範圍 形式:'=',但是當我訪問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; 
      } 
     } 
+0

是否有一個原因,爲什麼你的保存功能必須是形式之外? – SoEzPz

回答

1

在這裏你去:

Working Form

  • 表單按鈕是外的形式,也可能不在指令之內。這與Angularjs無關。

  • 有兩個輸入,雙方都需要並且都具有正則表達式驗證爲您說明。

  • 有一個templateURL

這裏要記住的重要事情指令是形式必須有一個名稱,然後就由該名稱引用爲:scope.myForm時

你不必爲我在plunker那樣來命名輸入字段,但如果你從互相幫助,而且都是不同的值,那麼你可以這樣做:scope.myForm.myInputName $有效的如果你願意,看看每個輸入是否有效。

但是,實際的形式將不再有效,直到所有的輸入是有效的,所以你可能只需要在提供的示例調用形式本身有效的。

如果將按鈕移動到指令外部,則必須將提交功能從指令移動到控制器(最有可能)。

讓我知道如果這有幫助,我可以改變事情,如果需要的話。另外,首先嚐試一下你的問題,然後發佈新的代碼,只是分贓並提供一個鏈接。

這是以防萬一的plunker代碼...

<!DOCTYPE html> 
    <html> 

    <head> 
    <link data-require="[email protected]*" data-semver="3.2.0" rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.2.0/css/bootstrap.css" /> 
    <script data-require="[email protected]*" data-semver="2.1.1" src="//cdnjs.cloudflare.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> 
    <script data-require="[email protected]*" 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 
});