Empty action attribute on a form with angularJS

前端 未结 4 751
我在风中等你
我在风中等你 2021-01-13 10:01

I\'m trying to submit a form the normal way in a AngularJS application but I encounter an issue : it seems that I must specify the action attribute.

According to the

4条回答
  •  臣服心动
    2021-01-13 10:16

    I created a small directive to solve this:

    .directive('form', ['$location', function($location) {
        return {
            restrict:'E',
            priority: 999,
            compile: function() {
                return {
                    pre: function(scope, element, attrs){
                        if (attrs.noaction === '') return;
                        if (attrs.action === undefined || attrs.action === ''){
                            attrs.action = $location.absUrl();
                        }
                    }
                }
            }
        }
    }]);
    

    Seems to be good for me. It looks for a form where the action is empty, and sets it to the current url.

    Actually, it doesn't set the action - it sets the attr value, so the actual form directive thinks it's got one.

    Update by @Reimund is good - I have actually had to do the same.

    New Update - I have added the option to add a noaction attribute to the form element; this enables you to return to a "normal" angular situation. Otherwise this directive will submit forms twice if using ajax.

提交回复
热议问题