仅在提交或用户输入时验证表单字段

2023-12-13

我有使用验证的表单字段required。问题是,呈现表单时会立即显示错误。我希望它仅在用户实际在文本字段中输入或提交后显示。

我怎样才能实现这个?


Use $dirty仅在用户与输入交互后才显示错误的标志:

<div>
  <input type="email" name="email" ng-model="user.email" required />
  <span ng-show="form.email.$dirty && form.email.$error.required">Email is required</span>
</div>

如果您想仅在用户提交表单后触发错误,则可以使用单独的标志变量,如下所示:

<form ng-submit="submit()" name="form" ng-controller="MyCtrl">
  <div>
    <input type="email" name="email" ng-model="user.email" required />
    <span ng-show="(form.email.$dirty || submitted) && form.email.$error.required">
      Email is required
    </span>
  </div>

  <div>
    <button type="submit">Submit</button>
  </div>
</form>
function MyCtrl($scope){
  $scope.submit = function(){
    // Set the 'submitted' flag to true
    $scope.submitted = true;
    // Send the form to server
    // $http.post ...
  } 
};

然后,如果里面都是 JSng-show表达式对你来说看起来太多了,你可以将它抽象成一个单独的方法:

function MyCtrl($scope){
  $scope.submit = function(){
    // Set the 'submitted' flag to true
    $scope.submitted = true;
    // Send the form to server
    // $http.post ...
  }

  $scope.hasError = function(field, validation){
    if(validation){
      return ($scope.form[field].$dirty && $scope.form[field].$error[validation]) || ($scope.submitted && $scope.form[field].$error[validation]);
    }
    return ($scope.form[field].$dirty && $scope.form[field].$invalid) || ($scope.submitted && $scope.form[field].$invalid);
  };

};
<form ng-submit="submit()" name="form">
  <div>
    <input type="email" name="email" ng-model="user.email" required />
    <span ng-show="hasError('email', 'required')">required</span>
  </div>

  <div>
    <button type="submit">Submit</button>
  </div>
</form>
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

仅在提交或用户输入时验证表单字段 的相关文章

  • 从子 ng-repeat 访问父 ng-repeat 的索引

    我想使用父列表 foos 的索引作为子列表 foos bars 中函数调用的参数 我发现有人建议使用 parent index 的帖子 但是 index不是以下的财产 parent 如何访问父级的索引ng repeat div div di
  • AngularJS 中的重定向状态

    这是状态配置 angular module grabhutApp config function stateProvider urlRouterProvider stateProvider ACCOUNT state account abs
  • Angular JS未知提供者错误

    删除 Bower components 并清理缓存后 我使用 Bower install 重新安装了依赖项 该应用程序无法加载并出现以下错误 未捕获的错误 injector unpr 未知提供程序 forceReflowProvider 这
  • Protractor addMockModule 附加参数不起作用?

    这看起来非常非常简单 但我不明白为什么这个简单的代码不起作用 我正在添加一个模拟模块来在 Angular E2E 测试中模拟我的 API 后端 我正在使用量角器 1 6 0 我需要将附加参数传递给模拟模块 根据 Protractor 文档
  • ngRepeat 中的指令时的绑定问题

    这就是它的样子 这是Plunker http plnkr co edit IPwDLT p preview parent scope ng repeat directive 在指令中 有一个属性与父作用域中的变量进行双向绑定 但这并没有像我
  • Angular JS - 使服务可以从控制器和视图全局访问

    假设我们有以下服务 myApp factory FooService function 然后 从控制器中 我会说 myApp controller FooCtrl scope FooService function scope FooSer
  • AngularJs ng-if 比较日期

    我正在比较 ng 中的两个日期 如果这就是我的玉文件的样子 li list group item ng if app Segments 0 StartDate getTime gt date getTime div row div col
  • AngularJS:服务、提供商、工厂

    之间有什么区别Service Provider and Factory在 AngularJS 中 从我得到的 AngularJS 邮件列表一个惊人的线程 https groups google com forum msg angular 5
  • 使用 ng-blur 和 ui-sref 无法按预期工作

    我有一个带有自定义下拉结果面板的搜索字段 在其中输入单词或聚焦时会显示该面板 所以我的 html 看起来像这样 div class input group div
  • 如何在 AngularJS 中滚动到页面顶部?

    我想在使用 angularjs 获得 ajax 调用响应后滚动到页面顶部 基本上 我在页面顶部显示警报消息 并且希望在收到 ajax 响应时将警报消息集中显示 Thanks 您可以使用 window scrollTo x y where x
  • AngularJS - 在等待数据/数据计算时加载图标

    我有一个简单的 Angular http get app factory countriesService function http return getCountryData function done http get resourc
  • 如何将一个“模型”映射到两个字段?

    我有一个具有 高度 属性的模型 我希望允许用户将其编辑为两个单独的字段 英尺 和 英寸 但让它们映射到以英寸为单位测量的单个属性 高度 表格看起来像这样 在这些字段和单个 高度 属性之间创建双向绑定的最佳方法是什么 HTML
  • 使用 AngularJS 制作 Windows Phone 全景图

    我正在尝试在 AngularJS 应用程序中复制 Windows Phone Ui 这是一个example http www expertreviews co uk gallery features 1295629 designing wi
  • Angular-Datatables + Angular-xeditable:取消可编辑行

    当组合 Angular DataTables 和 Angular XEditable 时 添加新行时会取消可编辑行 这是jsfiddle https jsfiddle net faj61h5d 10 示例操作如下 1 这是初始状态 2 将第
  • 监听指令中的表单提交事件

    我想监听指令中提交的表单 假设我有这样的指令 app directive myDirective function return restrict A require form scope smth link function scope
  • 使用 name 属性的动态指令控制器

    我正在尝试使用动态控制器实现指令 以便我可以根据某些条件绑定控制器 就像托德 莫托 Todd Motto 所展示的那样here https toddmotto com dynamic controllers in directives wi
  • 在 Angular 中,promise 的 error 和 catch 函数在概念上有什么区别?

    我终于得到了 Angular Promise 错误处理 但这对我来说是违反直觉的 我期望错误由失败回调来处理 但我不得不使用 catch 我在概念上不太明白为什么执行 catch 而不是失败回调 我所期望的 SomeAsyncService
  • 使用 eclipse IDE 配置 angularjs

    我想开始使用 AngularJs 和 Java Spring 进行开发 我使用 Eclipse 作为 IDE 我想配置我的 Eclipse 以使这些框架无缝工作 我知道我可能要求太多 但相信我 我已经做了很多研究 你们是我最后的选择 任何帮
  • 让 prerender.io 与 Facebook 爬虫(maven、GAE)一起使用?

    我有一个 angularjs 应用程序 我想在 Facebook 上分享页面 这是通过元标签处理的 https developers facebook com docs sharing best practices https develo
  • 如何将 PHP 与 yeoman Angular 项目集成

    我在用着yeoman questions tagged yeoman项目使用角js questions tagged angularjs通常我知道如何在普通项目中将 angularjs 与 PHP 结合使用 但我很困惑如何将 php 与 y

随机推荐