AngularJS Disabling Forms When Controllers are Created

孤街醉人 提交于 2019-12-12 04:37:51

问题


AngularJS disables (cannot submit) forms that do not have a specific action set:

<form action="">

when you create a controller. It does not have this issue when creating directive or factories.

You can see this in a plunk here:

http://plnkr.co/edit/gWFRMKGO3FzZtOgs4VmW?p=preview

Form is defined as:

<form action="" method="post">

If you delete the starting on line 6, you will be able to submit the form.

A simple solution is to define the action, but I'd rather not do this, as it is not necessary.

UPDATE

Some details can be found here on trying to get this change in Angular:

https://github.com/angular/angular.js/pull/3776


回答1:


You can use ng-submit to handle that.

<form ng-submit="submitForm()" method="post">
  <input name="test" value="11111111" />
  <button type="submit" name="submit" value="1">Send</button>
  <input type="submit" name="submit2" value="Send2" />
</form>

That way the form will submit normally and you must actually send the data on your submitForm function (just an example name).

Here is a quick plnkr: http://plnkr.co/edit/lwWVG0CDHSGMtMU0B8Nj?p=preview

Notice that you can submit using the buttons and also by pressing enter on the field. I hope that's what you've been asking for.

Thanks



来源:https://stackoverflow.com/questions/25823716/angularjs-disabling-forms-when-controllers-are-created

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!