且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

ng-click刷新页面而不是提交

更新时间:2023-10-29 10:41:34

最简单的解决方法是从调用add()方法的按钮中删除type="submit",将其替换为type="button"

Easiest way to fix this is to remove type="submit" from your button that calls the add() method, replacing it with type="button"

 <input type="button" value="Save" ng-click='add()' class="btn btn-primary col-sm-offset-3" /> 

否则,您需要在表单上添加ng-submit="add()",然后使用type = submit从按钮中删除ng-click="add()".

Otherwise, you need to add ng-submit="add()" on your form, and then remove the ng-click="add()" from your button with type=submit.

您的表单应如下所示:

 <form class="form-horizontal" ng-controller="MyAddController" ng-submit="add()">

您的按钮是这样的:

<input type="submit" value="Save" class="btn btn-primary col-sm-offset-3" /> 

这两种方法都应该起作用.

Either of these should work.

顺便说一句,您不需要使用;在ng-click指令的末尾.另外,请尝试使用按钮标记代替保存按钮的输入.

By the way, you don't need to use ; at the end of an ng-click instruction. Also, try using the button tag instead of input for the save button.

您忘记了声明要在html中使用的应用程序.在HTML代码的正文上,使用ng-app='myApp'

You forgot to declare the app to use in your html. On the body of your HTML code, use ng-app='myApp'