简体   繁体   English

防止AngularJS中的默认事件无法正常工作

[英]Preventing default event in AngularJS doesn't work correctly

Here is my HTML form: 这是我的HTML表单:

<form class="form-login" name="loginForm" novalidate ng-submit="login()">
    <fieldset>
        <h2>Login</h2>
        <input type="text" required class="input-block-level" placeholder="Username" ng-model="username" name="username" tabindex="1" xs-input-sync>
        <input type="password" required class="input-block-level" placeholder="Password" ng-model="password" name="password" tabindex="2" xs-input-sync>

    </fieldset>
</form>

and here's the controller method called when submitting the form: 这是提交表单时调用的controller方法:

$scope.login = function() {
        $http.post($scope.serviceUrls.getAuthenticationUrl($scope.username), {
            "password": $scope.password
        }).success(function(data, status, headers) {


        }).error(function(data, status) {

        });
        return false;
    }

Everything works fine up to this point. 到目前为止,一切正常。 But since the browser only shows the native "Remember Password" dialog only if you have the action="/" attribute set on the form, I've also added it on my form. 但是由于仅当您在表单上设置了action="/"属性时,浏览器才显示本机的“记住密码”对话框,因此我也将其添加到了表单中。

After adding the action="/" the form gets submitted when hitting the Enter key or the Login button no matter what I try. 在添加action="/" ,无论我如何尝试,按Enter键或“登录”按钮都将提交表单。 I've also tried to add the $event parameter to the html login function call, and then do $event.preventDefault(); 我还尝试将$event参数添加到html login函数调用中,然后执行$event.preventDefault(); but still no go. 但仍然没有去。

The same thing happens on urls like: 同样的事情发生在URL上:

<a class="xs-btn edit-btn" ng-click="toggleEditMode($event)"></a>

I've tried: $event.preventDefault(); 我试过了: $event.preventDefault(); , return false; return false; , adding href="" , href="#" . ,添加href=""href="#" But none of the work and I always get redirected to the root of the site. 但是没有任何工作,我总是被重定向到站点的根目录。 The only one that works is href="javascript:void(0);" 唯一有效的方法是href="javascript:void(0);" .

Any idea of what I'm doing wrong? 有什么想法我做错了吗? It seems like something is wrong app wide. 应用范围似乎有些错误。

如我所见,只需将onsubmit="return false"<form>完成工作: http : onsubmit="return false" preview

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

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