zoukankan      html  css  js  c++  java
  • 简单的angular表单验证指令

    <html ng-app="myApp">
    
    <head>
      <meta charset="UTF-8">
      <title>test表单验证</title>
      <script type="text/javascript" src="lib/angular/angular.js"></script>
      <script type="text/javascript">
      var app = angular.module('myApp', []);
      app.controller('testCtrl', function($scope) {
        var _test = function() {
          $scope.canSubmit = false;
          console.log('a');
        };
    
        var init = function() {
          $scope.model = {
            name: 'happen'
          };
          $scope.canSubmit = true;
          $scope.test = _test;
        };
        init();
      });
      app.directive('testValid', function() {
        return {
          restrict: 'A',
          require: 'ngModel',
          link: function(scope, elem, attrs, ctrl) {
            var init = function() {
              elem.on('blur', function() {
                scope.$apply(function() {
                  if (elem.val() === 'happen') {
                    ctrl.$setValidity('isHappen', false);
                  } else {
                    ctrl.$setValidity('isHappen', true);
                  }
    
                });
    
              });
            };
            init();
          }
        }
      });
      </script>
    </head>
    
    <body>
      <form name="myForm">
        <div ng-controller="testCtrl">
          <input type="text" name="test" ng-model="model.name" test-valid required>
          <p ng-show="myForm.test.$error.isHappen">名字是默认值</p>
          <p ng-show="myForm.test.$error.required">名字必填项</p>
          <button ng-disabled="myForm.$invalid || !canSubmit" ng-click="test();" style=" 200px;height: 20px;"></button>
        </div>
      </form>
    </body>
    
    </html>
  • 相关阅读:
    20199106 2019-2020-2 《网络攻防实践》第三周作业
    Vulnhub
    NEEPU-CTF 2021 Web后四题Writeup
    Vulnhub
    [VNCTF 2021]naive题解
    F5杯 Web部分题目Writeup by atao
    CTFSHOW SSTI 刷题
    C语言文件
    函数+进制转换器
    C语言知识点小结
  • 原文地址:https://www.cnblogs.com/happen-/p/5424209.html
Copyright © 2011-2022 走看看