您的位置:首页 > 其它

表单验证

2016-05-22 15:51 225 查看
处理验证,重复name的方法

html代码:

<!doctype html>

<html >
<head>
<meta charset="utf-8"/>
<title>hah</title>
<link rel="stylesheet" href="bootstrap.css">
<script src="angular.min.js"></script>
<script src="script.js"></script>
</head>

<body ng-app="myModule" ng-controller="scriptContr">
<div class="form-group" ng-submit="submitForm(userForm.$valid)">

<!-- 方法一 -->
<form name="userForm" novalidate>
<div ng-repeat = "user in users">
<ng-form name="aa">
<label>
{{user.name}} 's Email
</label>

<input type = "text" class="from-control" name ="email" ng-model="user.email" required />
<p  class= "help-block" ng-show="aa.email.$invalid && aa.email.$dirty">worry</p>
</ng-form>
</div>
</form>

</div>
<div>

<!--  方法二 -->
<form name="userForm1" novalidate>
<div ng-repeat="item in users">
 <label>
{{item.name}} 's Email</label>

          <input type="number"  dy-name="item.field" ng-model="user.email" min="10" max="500" required/>
 <p  class= "help-block" ng-show="userForm1[item.field].$invalid && userForm1[item.field].$dirty">worry</p>

       </div>
</form>
</div>
</body>

</html>

js 代码:

var myModule = angular.module("myModule",[]);

myModule.controller("scriptContr",['$scope',function($scope){
$scope.message = "here";
$scope.users=[
{
name:"no1",
email:'',
field:'no1'
},{
name:"no2",
email:'',
field:'no2'
}

]

}])

myModule.directive("dyName", [

        function() {

          return {

            require: "ngModel",

            link: function(scope, elm, iAttrs, ngModelCtr) {

              ngModelCtr.$name = scope.$eval(iAttrs.dyName)

              var formController = elm.controller('form') || {

                $addControl: angular.noop

              };

              formController.$addControl(ngModelCtr);

              scope.$on('$destroy', function() {

                formController.$removeControl(ngModelCtr);

              });

            }

          };

        }

      ])


细说angular Form addControl方法

          
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  表单验证 name