简体   繁体   English

模糊的Angular ngMessages验证会取消链接/按钮点击

[英]Angular ngMessages validation on blur cancels link/button clicks

I have a simple log in form with a username field (required), a Log In button, a Cancel button and a Forgot Password link. 我有一个简单的登录表单,其中包含用户名字段(必填),登录按钮,取消按钮和忘记密码链接。 The username field has focus on page load. 用户名字段的重点是页面加载。

I'm using ngMessages to display validation errors and in this simple example the form's only validation rule is username required. 我正在使用ngMessages来显示验证错误,在这个简单的示例中,表单的唯一验证规则是用户名。 I do not want to show the validation error on page load, only after the user leaves the username field. 只有在用户离开用户名字段后,我才不想在页面加载时显示验证错误。 To do this I've set the username required message to display only after the username element registers as touched. 为此,我设置了所需的用户名消息,只有在用户名元素注册为触摸后才显示。

My problem comes when a user clicks the Cancel button or the Forgot Password link, the validation message for username required is displayed as expected however the click event for the button or link never gets fired. 我的问题出现在用户单击“取消”按钮或“忘记密码”链接时,所需的用户名验证消息将按预期显示,但按钮或链接的单击事件永远不会被触发。 This is of course the correct behavior for the submit button but not for every other (link/button) element on the page. 这当然是提交按钮的正确行为,但不适用于页面上的每个其他(链接/按钮)元素。

It doesn't seem like ngMessages should be determining whether click events should be cancelled. 似乎ngMessages不应该确定是否应该取消点击事件。 While that is the correct behavior in this case for the submit button, I prefer to manage that myself. 虽然在这种情况下这对于提交按钮是正确的行为,但我更喜欢自己管理它。 Is this a bug or have I incorrectly implemented ngMessages, or something else? 这是一个错误还是我错误地实现了ngMessages或其他什么?

Edited to fully clarify the question being asked Why does the display of a validation message cause an unrelated click event to be cancelled and how can I prevent this? 编辑以完全澄清被问到的问题为什么显示验证消息会导致无关的点击事件被取消,我该如何防止这种情况?

Here's a plunker: https://plnkr.co/edit/bLWODaWSXowZ4AcgTze9?p=preview 这是一个吸烟者: https ://plnkr.co/edit/bLWODaWSXowZ4AcgTze9 p = preview

Edited to add: In the Plunkr, once the page loads, if a user clicks on the Forgot Password link, the validation error message Username Required should appear AND the Forgot Password click event should fire displaying a message on the page that says "Go to password reset". 编辑添加:在Plunkr中,一旦页面加载,如果用户单击忘记密码链接,则应显示验证错误消息Username Required,并且忘记密码单击事件应该触发在页面上显示“转到”的消息重设密码”。 This click event doesn't happen unless you click the Forgot Password link a second time. 除非您再次单击“忘记密码”链接,否则不会发生此单击事件。

Here's the code: 这是代码:

<form name="buttonFail" novalidate autocomplete="off">
    <label for="username">Username</label>
    <div>
        <input ng-model="ngUsername" id="username" name="username" type="text" required class="ngMessageSample" autofocus />
        <div ng-messages="buttonFail.username.$error" class="ngMessagesClass" ng-show="buttonFail.username.$touched">
            <div ng-message="required" class="ngMessageClass">* This field is required</div>
        </div>
    </div>
    <button type="submit" ng-click="//do nothing">Log In</button>
    <button type="button" ng-click="ngModel.saySomething='Log in cancelled';">Cancel</button>
    <br />
    <a href="#" ng-click="ngModel.saySomething='Go to password reset';">Forgot password?</a>
    <div>{{ngModel.saySomething}}</div>
</form>

It happens because in your code you have set autofocus attribute to the input.So the input will be focused on load itself.So when you first click the Password Reset or the Cancel button or anywhere the input blur event happens and .So the validation messgae will show . 这是因为在你的代码中你已经为输入设置了autofocus属性。所以输入将集中在加载本身。所以当你第一次点击密码重置或取消按钮或输入模糊事件发生的任何地方时。所以验证messgae将会呈现 。 The Password Reset or the Cancel button is moved by the appearance of the validation message and the click event location is changed before the click event completes .You can fix that by using ng-mousedown instead of ng-click 密码重置或取消按钮由验证消息的外观移动,并且单击事件位置在单击事件完成之前更改。您可以使用ng-mousedown而不是ng-click来修复它

Also remove ng-click="//do nothing" from your submit button 同时从提交按钮中删除ng-click="//do nothing"

https://plnkr.co/edit/gNR1yrXA6glXGpn9sH2h?p=preview https://plnkr.co/edit/gNR1yrXA6glXGpn9sH2h?p=preview

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

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