ng-action does not add action attribute in the form

后端 未结 5 1984
醉梦人生
醉梦人生 2020-12-18 20:09

I want to have dynamic action attribute in the form. I have a code

 

Angular d

相关标签:
5条回答
  • 2020-12-18 20:44

    For Angular 1.2+, you'll need to trust the URL with SCE.

    In the controller, add a dependency on $sce. Then, define the url as below

    $scope.formUrl = $sce.trustAsResourceUrl('/users/' + user.id);
    

    Then in the view

    <form action="{{formUrl}}" method="POST">
    
    0 讨论(0)
  • 2020-12-18 20:44

    Here is my solution using Angular directives:

    JS:

    app.controller('formCtrl', ['$scope', function($scope) {
        $scope.customAction = 'http://stackoverflow.com/';
    }])
    .directive('dynamicFormAction', function() {
        return {
            restrict: 'A',
            scope: {
                'dynamicFormAction': '='
            },
            link: function(scope, el, attrs) {
                scope.$watch('dynamicFormAction', function (action) {
                    el[0].action = action;
                });
            }
        };
    });
    

    HTML:

    <form action="" method="POST" dynamic-form-action="customAction">
         ...
    </form>
    
    0 讨论(0)
  • 2020-12-18 20:45

    You can use jQuery to change the action attribute, as the following example:

    $('#formId').attr('action', '/users/'+$scope.user.id);
    
    0 讨论(0)
  • 2020-12-18 20:47

    There is no directive called ng-action in Angular

    refer Angular DOCS

    <form action="{{'/users/' + user.id }}">
    

    You need to add above tag for that to work

    0 讨论(0)
  • 2020-12-18 20:51

    As of version 1.2, Angular ships with Strict Contextual Escaping (SCE) enabled by default.

    See https://docs.angularjs.org/api/ng/service/$sce

    0 讨论(0)
提交回复
热议问题