我有一个形式在Angular有两个按钮标签。一个按钮在ng单击时提交表单。另一个按钮纯粹用于使用ng单击导航。然而,当第二个按钮被点击Angularjs导致一个页面刷新触发一个404.我已经在函数中断了一个断点,它触发我的功能。如果我做任何以下它停止:
>如果我删除ng单击该按钮不会导致页面刷新。
>如果我注释掉的函数中的代码,不会导致页面刷新。
>如果我将按钮标签更改为具有href =“”的锚标签(a),则不会导致刷新。
后来是最简单的解决方法,但为什么Angular甚至在我的函数后运行任何代码,导致页面重新加载?看起来像一个错误。
这里是形式:
<form class="form-horizontal" name="myProfile" ng-switch-when="profile">
<fieldset>
<div class="control-group">
<label class="control-label" for="passwordButton">Password</label>
<div class="controls">
<button id="passwordButton" class="secondaryButton" ng-click="showChangePassword()">Change</button>
</div>
</div>
<div class="buttonbar">
<button id="saveProfileButton" class="primaryButton" ng-click="saveUser()">Save</button>
</div>
</fieldset>
</form>
这里是控制器方法:
$scope.showChangePassword = function() {
$scope.selectedLink = "changePassword";
};
如果你看看
W3C specification,看起来似乎很明显的事情是,当你不希望他们提交时,用type =’button’标记你的按钮元素。
特别要注意的是它在哪里说
A button element with no type attribute specified represents the same thing as a button element with its type attribute set to “submit”