简体   繁体   English

ng-show不显示输入无效时

[英]ng-show Not Showing When input is invalid

I am having trouble with form validation in AngularJS. 我在AngularJS中进行表单验证时遇到麻烦。 I am trying to show a div based on whether an input is invalid. 我试图根据输入是否无效显示一个div。 Angular is putting the correct classes on my inputs (ng-touched and ng-invalid) but it is still keeping the div with a ng-hide class. Angular在我的输入上放置了正确的类(ng-touched和ng-invalid),但仍将div保留为ng-hide类。

form.html form.html

                <form id="" name="contactForm"
                ng-submit="postForm()" novalidate>
                <div class="form-group row">
                    <label for="full-name" class="col-lg-2 col-md-3 col-xs-4 col-form-label">Name</label>
                    <div class="col-lg-10 col-md-9 col-xs-8">
                        <input class="form-control" type="text" placeholder="Jon Doe"
                            id="full-name" required data-ng-model="formData.name">
                    </div>
                </div>
<!-- This is the only input implemented with the errors -->
                <div class="form-group row">
                    <label for="phone-number" class="col-lg-2 col-md-3 col-xs-4 col-form-label">Phone
                        Number</label>
                    <div class="col-lg-10 col-md-9 col-xs-8">
                        <input class="form-control" type="tel"
                            placeholder="(630) 555-6723" id="phone-number" required
                            data-ng-model="formData.phone" name="phone" ng-minlength="10"/>
                            </div>
                        <div ng-show="(contactForm.phone.$invalid && contactForm.phone.$touched) || contactForm.phone.$error.minlength">Please enter a valid phone
                            number.</div>

                </div>
                <div class="form-group row">
                    <label for="email" class="col-lg-2 col-md-3 col-xs-4 col-form-label">Email</label>
                    <div class="col-lg-10 col-md-9 col-xs-8">
                        <input class="form-control" type="email"
                            placeholder="jon.doe@gmail.com" id="email" required
                            data-ng-model="formData.email" />
                        <div class="invalid-feedback">Please enter a valid email.</div>
                    </div>
                </div>
                <div class="form-group row">
                    <label class="col-2 col-form-label">Contact Method</label>
                    <div class="col-lg-2 col-md-3 col-xs-4">
                        <label class="custom-control custom-radio"> <input
                            id="radio1" name="email" type="radio"
                            class="custom-control-input" checked="checked"
                            data-ng-model="formData.pref"> <span
                            class="custom-control-indicator"></span> <span
                            class="custom-control-description">Email</span>
                        </label> <label class="custom-control custom-radio"> <input
                            id="radio2" name="phone" type="radio"
                            class="custom-control-input" data-ng-model="formData.pref">
                            <span class="custom-control-indicator"></span> <span
                            class="custom-control-description">Phone</span>
                        </label>
                    </div>
                </div>
                <div class="form-group row">
                    <label for="full-name" class="col-lg-2 col-md-3 col-xs-4 col-form-label">Body</label>
                    <div class="col-lg-10 col-md-9 col-xs-8">
                        <textarea rows="15" class="form-control"
                            placeholder="Type your message here..." id="body-text" required
                            data-ng-model="formData.body"></textarea>
                        <div class="invalid-feedback">Please enter a message here.</div>
                    </div>
                </div>
                <div class="text-center">
                    <button ng-disabled="contactForm.$invalid" class="btn-primary btn" type="submit">Submit</button>
                </div>
            </form>
</div>

angular.js angular.js

    <!-- SCRIPTS -->
<script>
    //form validate

    var app = angular.module('contactUs', []);
    app.controller('formCtrl', function($scope, $http) {
        $scope.formData = {};
        $scope.postForm = function() {
            $('#loadingScreen').modal('show');
            $http({
                url : '/api/v1/contact',
                method : 'POST',
                data : $.param($scope.formData),
                headers: {'Content-Type': 'application/x-www-form-urlencoded'}
            })
            .then(function onSuccess() {
                console.log("success");
                window.scrollTo(0,0);
                $('#success-message').show();
            }, function onError(){
                console.log("error");
                window.scrollTo(0,0);
                $('#error-message').show();
            })
            .then(function() {
                $('#loadingScreen').modal('hide');
            })
        }
        $scope.formData = {};
    });
</script>

Looking at other errors I made sure that the ng-show was using the formName.inputName and that the app and controller were working correctly. 查看其他错误,我确保ng-show使用的是formName.inputName,并且应用程序和控制器运行正常。 The button is disabled correctly when the form is invalid which leads me to believe that the controller and app are not the problem. 当表单无效时,该按钮将被正确禁用,这使我认为控制器和应用程序不是问题。

I tried using ng-messages which changed my phone portion of the form to 我尝试使用ng-messages将表格的电话部分更改为

<div class="form-group row">
                    <label for="phone-number" class="col-lg-2 col-md-3 col-xs-4 col-form-label">Phone
                        Number</label>
                    <div class="col-lg-10 col-md-9 col-xs-8">
                        <input class="form-control" type="tel"
                            placeholder="(630) 555-6723" id="phone-number" required
                            data-ng-model="formData.phone" name="phone" ng-minlength="10"/>
                            </div>
                        <div ng-messages="contactForm.phone.$error" class="error" ng-if="contactForm.phone.$dirty">
                            <div ng-message="required">Please enter a phone number we can reach you at.</div>
                            <div ng-message="pattern">Please enter a valid phone number here.</div>
                            <div ng-message="minlength">Please enter a phone number that's atleast 10 digits</div>
                        </div>

                </div>

Angular is still adding the correct classes to the input but it is still not displaying. Angular仍在向输入中添加正确的类,但仍未显示。 I believe the ng-if is evaluating false. 我相信ng-if评估为假。 Oddly, when I run 奇怪的是,当我跑步时

{{contactForm.phone.$dirty}} 

in the console it is coming back as undefined 在控制台中,它作为未定义返回

Take a look here and you can see how I got it working. 在这里看看,您会看到我是如何工作的。 I'll break down the steps below. 我将分解以下步骤。 https://plnkr.co/edit/LJYurBObZsS6ptlVKxvP?p=preview https://plnkr.co/edit/LJYurBObZsS6ptlVKxvP?p=preview

With the change to using ng-messages, you need to include that module in your app. 随着使用ng-messages的更改,您需要在应用程序中包含该模块。 I wasn't sure if you had made this change or not, but it's straightforward. 我不确定您是否进行了此更改,但这很简单。 Include the script for the library, and then update your modules to include ngMessages as a dependency. 包括库的脚本,然后更新模块以将ngMessages作为依赖项包括在内。

var app = angular.module('contactUs', ['ngMessages']);

The second part is fixing a typo. 第二部分是修正拼写错误。 In your html for the contact method area, you have 在用于联系方式区域的html中,

<label class="custom-control custom-radio">
        <input id="radio2" name="phone" type="radio" class="custom-control-input" data-ng-model="formData.pref">
      <span class="custom-control-indicator"></span>
      <span class="custom-control-description">Phone</span>
</label>

You're reusing the name "phone" there, which is going to cause problems since you'll have two different form items with the same name. 您将在那里重复使用“电话”这个名称,这将引起问题,因为您将拥有两个具有相同名称的不同表单项。 Probably change those inputs to emailPreference and phonePreference or something to be clear. 可能将这些输入更改为emailPreference和phonePreference或一些需要明确的内容。

As soon as I cleaned that up, it started working. 我一清理干净,它便开始工作。 So I think you were all there, just this typo was getting you. 所以我想你就在那里,只是这个错字让你受了。

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

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