I want to have dynamic action attribute in the form. I have a code
Angular d
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">
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>
You can use jQuery to change the action attribute, as the following example:
$('#formId').attr('action', '/users/'+$scope.user.id);
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
As of version 1.2, Angular ships with Strict Contextual Escaping (SCE) enabled by default.
See https://docs.angularjs.org/api/ng/service/$sce