【问题标题】:AngularJS Slightly Advanced Validation / Bootstrap3 StyleAngularJS 稍微高级的验证/Bootstrap3 风格
【发布时间】:2013-12-08 08:30:14
【问题描述】:

我正在学习 AngularJS,还处于表单验证的最基本阶段。我关注official tutorial here,并设法让验证像他们一样工作,如果输入无效,背景会改变颜色。

这一切都很好,是我学习的重要一步,但我该如何更进一步,让验证添加/删除 Bootstrap 所需的 CSS 类以显示视觉提示?

这是我的 HTML 代码:

<form novalidate class="css-form">
  <div class="form-group">
    <label class="control-label" for="fname">First Name</label>
    <input type="text" id="fname" placeholder="First Name" class="form-control" ng-model="user.fname" required >
  </div>
  <div class="form-group">
    <label class="control-label" for="lname">Last Name</label>
    <input type="text" id="lname" placeholder="Last Name" class="form-control" ng-model="user.lname" required >
  </div>
  <div class="form-group">
    <label class="control-label" for="email">Email</label>
    <input type="email" id="email" placeholder="Email" class="form-control" ng-model="user.email" required >
  </div>
  <div class="form-group">
    <label class="control-label" for="password">Password</label>
    <input type="password" id="password" placeholder="Password" class="form-control" ng-model="user.password" required >
  </div>
  <div class="form-group">
    <label class="control-label" for="emailpref">Want annoying emails?</label>
    <select class="form-control" ng-model="user.emailpref" required>
      <option value="Null">Please Select</option>
      <option value="Yes">Yes</option>
      <option value="No">No</option>
    </select>
  </div>
  <button type="submit" class="btn btn-primary">Submit</button>
</form>

Bootstrap3 documentation 中,它说如果我需要显示有效状态,我必须添加一个has-success 的CSS 类,如下所示:

<div class="form-group has-success">
    <label class="control-label" for="fname">First Name</label>
    <input type="text" id="fname" placeholder="First Name" class="form-control" ng-model="user.fname" required >
</div>

我怎样才能让我的 AngularJS 验证来做到这一点?目前,我的AngularJS如下:

function UserCtrl($scope) {
    $scope.master = {};
    $scope.user   = { fname: "J", lname: "Watson", password: "test", email: "j.watson@world.com", emailpref: "Yes" };

    $scope.update = function(user) {
        $scope.master = angular.copy(user);
    };

    $scope.reset = function() {
        $scope.user = angular.copy($scope.master);
    };

    $scope.reset();
}

【问题讨论】:

标签: validation angularjs twitter-bootstrap-3


【解决方案1】:

要根据字段验证添加动态类,您需要做两件事

给你的表单一个name

 `<form novalidate class="css-form" name="form1">`

也给每个输入字段一个name。然后您可以使用表达式来确定字段的错误状态

<div class="form-group has-success" ng-class="{'has-success':form1.fname.$invalid}">
    <label class="control-label" for="fname">First Name</label>
    <input type="text" id="fname" placeholder="First Name" class="form-control" ng-model="user.fname" required name="fname">
</div>

请通过表单指南http://docs.angularjs.org/guide/forms了解更多详情。

【讨论】:

  • 您能解释一下ng-class="{'has-success':form1.fname.$invalid} 的作用吗?如果我不得不猜测,我会说当fname 字段无效时添加一个has-success 类!这对我来说没有意义!
  • 顺便说一句,我确实阅读了您提供的链接,那里没有提到您正在做的事情:(
  • 对不起,我颠倒了条件,但想法保持不变。该文档提供了类似的示例。搜索表达式用法liekform.uEmail.$dirty
【解决方案2】:

您可以使用angular-validator

免责声明:我是angular-validator的作者

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-01
    • 1970-01-01
    • 1970-01-01
    • 2021-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-19
    相关资源
    最近更新 更多