javascript ng-action 没有在表单中添加 action 属性

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/23661299/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-28 01:20:18  来源:igfitidea点击:

ng-action does not add action attribute in the form

javascriptangularjs

提问by Fizer Khan

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

我想在表单中有动态动作属性。我有一个代码

 <form ng-action="/users/{{user.id}}">
 </form>

Angular does replaces {{user.id}}with the actual value, but it does not add actionattribute with the new value. How do i fix this?

Angular 确实会替换{{user.id}}为实际值,但不会action使用新值添加属性。我该如何解决?

I also tried with

我也试过

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

It does working in Angular 1.2.1, but not in higher version (>1.2.1)

它确实适用于 Angular 1.2.1,但不适用于更高版本(>1.2.1)

  1. JSFiddle with angular version 1.2.1, http://jsfiddle.net/fizerkhan/s8uCT/5/
  2. JSFiddle with angular version 1.2.2, http://jsfiddle.net/fizerkhan/s8uCT/6/
  1. 带有角度版本的 JSFiddle 1.2.1http://jsfiddle.net/fizerkhan/s8uCT/5/
  2. 带有角度版本的 JSFiddle 1.2.2http://jsfiddle.net/fizerkhan/s8uCT/6/

I also tried with angular version 1.2.4, 1.2.6, it does not work.

我也试图与角版本1.2.41.2.6,这是行不通的。

回答by mohamedrias

There is no directive called ng-action in Angular

Angular 中没有名为 ng-action 的指令

refer Angular DOCS

参考 Angular DOCS

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

You need to add above tag for that to work

你需要添加上面的标签才能工作

回答by Loren

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

对于 Angular 1.2+,您需要使用 SCE 信任 URL。

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

在控制器中,添加对$sce. 然后,定义url如下

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

Then in the view

然后在视图中

<form action="{{formUrl}}" method="POST">

回答by BrianS

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

从 1.2 版开始,Angular 默认启用了严格上下文转义 (SCE)。

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

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

回答by M. Stein

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

您可以使用 jQuery 来更改 action 属性,如下例所示:

$('#formId').attr('action', '/users/'+$scope.user.id);

回答by Stalinko

Here is my solution using Angular directives:

这是我使用 Angular 指令的解决方案:

JS:

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:

HTML:

<form action="" method="POST" dynamic-form-action="customAction">
     ...
</form>