如何在AngularJS中停止事件传播

时间:2014-06-04 11:09:49

标签: javascript jquery 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";

}

});

1 个答案:

答案 0 :(得分:2)

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

将您的代码更改为:

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