2016-12-06 9 views
0

私のコードスニペットをご覧ください。スプライシングでフォームエラーが削除されました。ng-repeat配列アイテム

<!DOCTYPE html> 
 
<html data-ng-app="myApp"> 
 

 
<head> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 
 
</head> 
 

 
<body data-ng-controller="testController"> 
 
    <ng-form name="phoneInnerForm"> 
 
    <div> 
 

 
     <div class="form-group" data-ng-repeat="item in items" ng-class="phoneInnerForm.phones{{$index}}.$error.maxlength ? 'phone_number_error': ''"> 
 
     <input type="text" class="form-control" id="InputAddPhone" name="phones{{$index}}" ng-model="item.number" ng-maxlength="50"> 
 
     <select class="form-control" ng-model="item.type"> 
 
      <option value=""></option> 
 
      <option value="mobile">Mobile</option> 
 
      <option value="work">Work</option> 
 
      <option value="home">Home</option> 
 
      <option value="fax">Fax</option> 
 
      <option value="other">Other</option> 
 
     </select> 
 

 
     <div class="evy_email_dltbtn"> 
 
      <button type="button" class="btn btn-default btn_delete" ng-click="deleteItem($index);" title="Delete">Delete</button> 
 
      <button ng-show="$last" type="button" class="btn btn-default btn_delete" ng-click="addItem();" title="Delete">Add</button> 
 
     </div> 
 
     <span ng-show="phoneInnerForm.phones{{$index}}.$error.maxlength" class="evy_user-preference_error">Phone number should not exceed 50 characters</span> 
 
     </div> 
 

 
    </div> 
 
    </ng-form> 
 
    <script> 
 
    angular 
 
     .module('myApp', []) 
 
     .controller('testController', ['$scope', 
 
     function($scope) { 
 

 
      $scope.items = [{ 
 
      number: "", 
 
      type: "" 
 
      }]; 
 

 
      $scope.addItem = function() { 
 
      $scope.items.push({ 
 
       number: "", 
 
       type: "" 
 
      }); 
 
      } 
 

 
      $scope.deleteItem = function(index) { 
 
      $scope.items.splice(index, 1); 
 
      } 
 

 
     } 
 
     ]); 
 
    </script> 
 
</body> 
 

 
</html>

50よりも大きい長さを有する3-4電話番号を追加してみてください[削除ボタンを使用して第1 PHONENUMBERを削除してみてください。 私の問題は、最後の電話番号の検証が削除されています。 私を助けてください。

答えて

1

下記を使用してください。私はng-repeatをng-repeatの中に入れ、インデックスはテキストフィールド名と検証表示から削除します。 Answer.Butこの溶液が出力複数のフォームと同じname.rightと入力フィールドの

<!DOCTYPE html> 
 
<html data-ng-app="myApp"> 
 

 
<head> 
 
    <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.8/angular.min.js"></script> 
 
</head> 
 

 
<body data-ng-controller="testController"> 
 
    
 
    <div> 
 

 
     <div class="form-group" data-ng-repeat="item in items" ng-class="phoneInnerForm.phones{{$index}}.$error.maxlength ? 'phone_number_error': ''"> 
 
     <ng-form name="phoneInnerForm"> 
 
     <input type="text" class="form-control" id="InputAddPhone" name="phones" ng-model="item.number" ng-maxlength="50"> 
 
     <select class="form-control" ng-model="item.type"> 
 
      <option value=""></option> 
 
      <option value="mobile">Mobile</option> 
 
      <option value="work">Work</option> 
 
      <option value="home">Home</option> 
 
      <option value="fax">Fax</option> 
 
      <option value="other">Other</option> 
 
     </select> 
 

 
     <div class="evy_email_dltbtn"> 
 
      <button type="button" class="btn btn-default btn_delete" ng-click="deleteItem($index);" title="Delete">Delete</button> 
 
      <button ng-show="$last" type="button" class="btn btn-default btn_delete" ng-click="addItem();" title="Delete">Add</button> 
 
     </div> 
 
     <span ng-show="phoneInnerForm.phones.$error.maxlength" class="evy_user-preference_error">Phone number should not exceed 50 characters</span> 
 
     </ng-form> 
 
     </div> 
 

 
    </div> 
 
    
 
    <script> 
 
    angular 
 
     .module('myApp', []) 
 
     .controller('testController', ['$scope', 
 
     function($scope) { 
 

 
      $scope.items = [{ 
 
      number: "", 
 
      type: "" 
 
      }]; 
 

 
      $scope.addItem = function() { 
 
      $scope.items.push({ 
 
       number: "", 
 
       type: "" 
 
      }); 
 
      } 
 

 
      $scope.deleteItem = function(index) { 
 
      $scope.items.splice(index, 1); 
 
      } 
 

 
     } 
 
     ]); 
 
    </script> 
 
</body> 
 

 
</html>

+0

おかげ? –

+0

はい、同じ名前ですが、各フォームは異なるものとして扱われます。 $ scope.itemsを使用して、各フィールドから値を取得できます。 ngFormは、要件のような入れ子の目的に使用するように設計されています。 –

+0

これは私のために働いています。これをupvoteしてください。 –

関連する問題