简体   繁体   English

ngClass和AngularJS的表单验证不起作用

[英]Form Validation with ngClass and AngularJS not working

I am new to angular and am trying (and failing) to get the ngClass to work with some validation on a basic contact form. 我是angular的新手,正在尝试(并且失败)让ngClass在基本联系表单上进行一些验证。

I want to highlight the inputs when they are in the success/error by highlighting with bootstrap 'has-error' and 'glyphicon-remove' classes if there are any issues with the data. 如果数据有任何问题,我想通过引导程序“ has-error”和“ glyphicon-remove”类突出显示输入是否成功/错误。

my form is as follows; 我的表格如下:

<form class="row instant-quote-form" ng-controller="formCtrl" ng-cloak>
                    <div class="col-md-12">
                        <h2>Quick Form</h2>
                        <div class="form-group has-feedback">
                            <div class="input-group col-md-12">
                                <label for="titleSelect" class="input-group-addon">Title :</label>

                                <select name="titleSelect" id="titleSelect" class="form-control"
                                        ng-model="form.titleResult" required>
                                    <option value="">Select Title...</option>
                                    <option ng-repeat="option in titles" value="{{option.name}}">{{option.name}}</option>
                                </select>
                            </div>
                            <div class="input-group col-md-12" ng-class="{ 'has-error': form.name.$invalid, 'text-success' : form.name.$valid}">
                                <label for="nameInput" class="input-group-addon">Name :</label>
                                <input type="text" class="form-control" id="nameInput" placeholder="Full Name..." ng-model="form.name" required>
                                <span ng-class="{ 'glyphicon-remove' : form.name.$invalid, 'glyphicon-ok' : form.name.$valid }" class="glyphicon form-control-feedback"></span>
                            </div>

                            <div class="input-group col-md-12">
                                <label for="postcodeInput" class="input-group-addon">Postcode :</label>
                                <input type="text" class="form-control" id="postcodeInput" placeholder="Postcode..." ng-model="form.postcode" required>
                                <span class="glyphicon form-control-feedback"></span>
                            </div>
                            <div class="input-group col-md-12">
                                <label for="emailInput" class="input-group-addon">Email :</label>
                                <input type="email" class="form-control" id="emailInput" placeholder="Email Address..." ng-model="form.email" required>
                                <span class="glyphicon form-control-feedback"></span>
                            </div>
                            <div class="input-group col-md-12">
                                <label for="telephoneInput" class="input-group-addon">Telephone :</label>
                                <input type="text" class="form-control" id="telephoneInput" placeholder="Telephone Number..." ng-model="form.telephone" required>
                                <span class="glyphicon form-control-feedback"></span>
                            </div>
                            <div class="input-group col-md-12">
                                <label for="timeSelect" class="input-group-addon">To to Call :</label>

                                <select name="timeSelect" id="timeSelect" class="form-control"
                                        ng-model="form.timeResult" required>
                                    <option value="">Select Time...</option>
                                    <option ng-repeat="option in times" value="{{option.name}}">{{option.name}}</option>
                                </select>
                            </div>
                            <div class="col-md-12">
                                <button type="submit" class="btn btn-primary btn-block" ng-click="submit(form)">Request Callback!</button>
                            </div>
                        </div>
                    </div>
                </form>

my controller is as follows; 我的控制器如下:

'use strict';
app.controller('formCtrl', ['$scope', '$http', function ($scope, $http) {

$scope.titles =
   [
  { id: '1', name: 'Mr.' },
  { id: '2', name: 'Mrs.' },
  { id: '3', name: 'Master.' },
  { id: '4', name: 'Miss.' },
  { id: '5', name: 'Sir.' },
  { id: '6', name: 'Dr.' },
  { id: '7', name: 'Other.' }
   ];

$scope.times = 
           [
  { id: '1', name: 'Morning.' },
  { id: '2', name: 'Afternoon.' }
           ];

$scope.submit = function (form) {

    console.log(form);
};

}]); }]);

However, the ngclass directive is not being applied? 但是,是否未应用ngclass指令? also it seems that the submit is being called even when the form is invalid? 即使表单无效,似乎提交也被调用了吗? do I need to validate the form fromthe controller as well? 我还需要从控制器验证表单吗?

Add a name attribute and value to your form tag & input... 将名称属性和值添加到表单标签并输入...

<form name="form" class="row instant-quote-form" ng-controller="formCtrl" ng-cloak>
&
<input name="name" type="text" class="form-control" id="nameInput" placeholder="Full Name..." ng-model="name" required>

because when you refer to form.name in 'has-error': form.name.$invalid it works based off of the name of the form and the name of the input box 因为当您在'has-error': form.name.$invalid引用form.name'has-error': form.name.$invalid会根据表单名称和输入框的名称起作用

Note there is a conflict using form.name as your ng-model based upon your controller you will want ng-model="name" 请注意 ,根据您的控制器,使用form.name作为ng-model会有冲突,您将需要ng-model="name"

Here is a working plnkr 这是一个工作的plnkr

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM