如何停止AngularJS中的事件传播

如何停止AngularJS中的事件传播

当我单击“注册”按钮时,它也将转到“登录”按钮的处理程序。我已经使用$ event.stopPropagation()作为“注册”按钮来避免它,但是它不起作用。

编码 -

<form role="form" ng-submit="login(userName, password)" class="form-horizontal">
    <div class="form-group">
      <label for="usrNm" class="col-sm-2 control-label">Username</label>
      <div class="col-sm-10">
        <input type="email" id="usrNm" ng-model="userName" class="form-control input-lg" placeholder="Enter email"/>
      </div>
    </div>
    <div class="form-group">
      <label for="pwd" class="col-sm-2 control-label">Password</label>
      <div class="col-sm-10">
        <input type="password" id="pwd" ng-model="password" class="form-control input-lg" placeholder="Password"/>
      </div>
    </div>
    <div class="form-group">
      <div class="col-sm-offset-2 col-sm-10">
        <button class="btn btn-default">LOGIN</button>
        <button class="btn" ng-click="register(); $event.stopPropagation();">SIGN UP</button>
      </div>
    </div>
  </form>


controller.js-

expmodule.controller('loginForm', function($scope, sharedProperties) {
  var auth;
$scope.login = function (userid, password) {
  auth = sharedProperties.session.isAuthenticated(userid, password);
  if (auth) {
    alert("Welcome "+userid);
    window.location = "home.html";
  } else {
    alert("Invalid Login");
  }
}
$scope.register = function () {
  window.location = "register.html";

}


});

最佳答案

<button>的默认行为是提交它们所属的表单。

将您的代码更改为此:

<button class="btn btn-default">LOGIN</button>
<button type="button" class="btn" ng-click="register();">SIGN UP</button>

关于javascript - 如何停止AngularJS中的事件传播,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24035849/

10-09 22:44