zoukankan      html  css  js  c++  java
  • angularJs form

    form.name.$valid是否有效

    form.name.$invalid 是否无效

    form.name.$error错误的集合

      form.name.$error.required 

      form.name.$error.email 

    控制submit按钮是否disabled 启用ng-disabled="form.$invalid"

    下面是验证是否用户名唯一的简单写法

    <!DOCTYPE html>
    
    <html>
    <head>
        <title></title>
        <script src="angular.min.js" type="text/javascript"></script>
    </head>
    <body ng-app="formTest" ng-controller="formController">
        <form name="myForm" ng-submit="show()">
            <input name="personName" required ng-model="person.name" ensure-Unique="personName"/>
            <span></span>
            <input type="submit" ng-disabled="myForm.$invalid"/>
        </form>
        <script>
            var formTest = angular.module("formTest", []);
            formTest.controller("formController", function ($scope) {
                $scope.person = {
                    email: "",
                    name: "Jackey"
                };
                $scope.show = function () {
                    //alert($scope.person.name);
                    console.log(myForm.personName.$error)
                };
            }).directive("ensureUnique", function ($http) {
                return {
                    require: "ngModel",
                    link: function (scope, element, attrs) {
                        scope.$watch(attrs.ngModel, function () {
                  //$http..... console.log(scope.person.name); element.next(
    "span").text(scope.person.name);//显示是否存在唯一 }); } }; }); </script> </body> </html>

     再修改一下

    <!DOCTYPE html>
    
    <html>
    <head>
        <title></title>
        <script src="angular.min.js" type="text/javascript"></script>
    </head>
    <body ng-app="formTest" ng-controller="formController">
        <form name="myForm" ng-submit="show()">
            <input name="personName" required ng-model="person.name" ensure-Unique="personName"/>
            <span></span>
            <input type="submit" ng-disabled="myForm.$invalid"/>
        </form>
        <script>
            var formTest = angular.module("formTest", []);
            formTest.controller("formController", function ($scope) {
                $scope.person = {
                    email: "",
                    name: ""
                };
                $scope.show = function () {
                    //alert($scope.person.name);
                    console.log(myForm.personName.$error)
                };
            }).directive("ensureUnique", function () {
                return {
                    require: "ngModel",
                    link: function (scope, element, attrs) {
                        scope.$watch(attrs.ngModel, function (n) {
                            if (!n) return;
                            console.log(n);
                            element.next("span").text(scope.person.name); //显示是否存在唯一
                        });
    
                    }
                };
            });
        </script>
    </body>
    </html>
  • 相关阅读:
    Vue 左右翻页,点赞动画
    gitbook 使用
    css3 序列帧动画抖动
    ios添加-webkit-overflow-scrolling依然卡顿
    css 多行省略号兼容移动端
    Vue粒子特效(vue-particles插件)
    css3 渐变色兼容移动端
    前端性能优化:客户端从输入到展示讲解
    前端通信:ajax设计方案(六)--- 全局配置、请求格式拓展和优化、请求二进制类型、浏览器错误搜集以及npm打包发布
    前端通信:ajax设计方案(五)--- 集成promise规范,更优雅的书写代码(改迭代已作废,移步迭代10)
  • 原文地址:https://www.cnblogs.com/lihaozhou/p/3674957.html
Copyright © 2011-2022 走看看