角度控制表格

时间:2014-11-18 09:26:55

标签: javascript angularjs angularjs-directive

所以我有这个表单摘录:

<form name="registerForm" novalidate role="form">
    <div class="row">
        <div class="small-3 columns"><label for="pwd" class="right inline">Password:</label></div>
        <div class="small-9 columns">
            <label class="" ng-class="{'error': registerForm.pwd.$error.required && registerForm.pwd.$dirty}"><input type="password" id="pwd" name="pwd" ng-model="registerDetails.password" ng-required="true" /></label>
            <small class="error" ng-show="registerForm.pwd.$error.required && registerForm.pwd.$dirty">Password Required</small>
        </div>
    </div>
</form>

正如您所看到的那样registerForm.pwd是表单名称加上输入名称。 Angular已将$error$dirty等内容添加到此变量中。

我有什么办法可以添加自己的吗?那么像registerForm.pwd.$notEqual通过指令吗?

1 个答案:

答案 0 :(得分:1)

是的,请参阅下面的workink演示。

&#13;
&#13;
var app = angular.module('app', []);
app.directive('validPasswordC', function() {
  return {
    require: 'ngModel',
    scope: {

      reference: '=validPasswordC'

    },
    link: function(scope, elm, attrs, ctrl) {
      ctrl.$parsers.unshift(function(viewValue, $scope) {
        if (viewValue === "") {
          ctrl.$setValidity('_required', false)
        } else {
          ctrl.$setValidity('_required', true)
        }

        var noMatch = viewValue != scope.reference
        ctrl.$setValidity('notEqual', !noMatch)
      });

      scope.$watch("reference", function(value) {;
        ctrl.$setValidity('notEqual', value === ctrl.$viewValue);

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




});
&#13;
<script src="//ajax.googleapis.com/ajax/libs/angularjs/1.3.2/angular.min.js"></script>
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.0/css/bootstrap.min.css" rel="stylesheet" type="text/css" />
<div class="container" ng-app="app">
  <form name="registerForm" novalidate role="form">
    <div class="row">
      <div class="small-3 columns">
        <label for="pwd" class="right inline">Password:</label>
      </div>
      <div class="small-9 columns">
        <label class="" ng-class="{'error': registerForm.pwd.$error.required && registerForm.pwd.$dirty}">
          <input type="password" id="pwd" name="pwd" ng-model="registerDetails.password" placeholder=" password" ng-required="true" />



        </label>
        <label for="pwd" class="right inline">Confirm Password:</label>
        <input type="password" id="password_c" name="pwdConfirm" ng-model="registerDetails.passwordConfirm" placeholder="confirm password" valid-password-c="registerDetails.password" ng-required="true" />

        <p class="error" ng-show="registerForm.pwd.$error.required && registerForm.pwd.$dirty">
          Password Required
        </p>
        <p ng-show="registerForm.pwdConfirm.$error.notEqual" class="error">
          Passwords do not match.
        </p>

        <p class="error" ng-show="registerForm.pwdConfirm.$error.required && registerForm.pwdConfirm.$dirty || registerForm.pwdConfirm.$error._required ">
          Confirmation Required
        </p>
      </div>
    </div>

  </form>
</div>
&#13;
&#13;
&#13;