0

I have a template like this:

<div><form id="appForm" method="POST" action="{{form.formActionAttr}}">
            <table class="form-table">
                <tr>
                    <td>
                        <input name="input1" type="hidden" value="{{form.input1}}"/>
                    </td>
                </tr>
                <tr>
                    <td>
                        <input name="input2" type="hidden" value="{{form.input2}}"/>
                    </td>
                </tr>
            </table>
        </form></div>

here is the controller:

function SamlFormController($scope, $sce, metadataService) {
    $scope.form = {};
    $scope.form.formActionAttr = $sce.trustAsResourceUrl('some.url');
    $scope.form.input1 = 'input1';
    $scope.form.input2 = 'input2';

    $scope.submitForm = function() {
        document.getElementById('appForm').submit();
    };
    $scope.submitForm();
}

Here is my intention: the controller run the submitForm() method, which send a HTTP POST request to the form.formActionAttr url.

But with the codes above, the action="{{form.formActionAttr}}" is not populated yet when the $scope.submitForm() is called.

How do I fix this issue?

Thanks in advance :-)

4
  • $sce.trustAsResourceUrl will return the trusted value in your context so should be instant. How did you check whether it is the target issue or something else? Commented Jan 16, 2018 at 22:11
  • @PM. when i run the page, it is redirected to something like: localhost:8080/{{form.formActionAttr}} and saying this page is not found Commented Jan 16, 2018 at 22:18
  • Why are you using a view if it's not doing anything, instead of just sending an http request with $http? Also, it's better to reference the form by the name attribute. If you set the name, it will be available on the scope under that property. Commented Jan 16, 2018 at 22:36
  • @CShark Basically I'm implementing a SAML flow. The view that does nothing but sending the POST is the last step of the SAML authorization. In order for Service Provider to set session cookies, I can't use $http Commented Jan 16, 2018 at 23:17

1 Answer 1

2

The problem is that the url will only be set after you application has really initialized. So you need to push the change to a later stage. You can easily do this using the $timeout service:

function SamlFormController($scope, $sce, $timeout, metadataService) {
    $scope.form = {};
    $scope.form.formActionAttr = $sce.trustAsResourceUrl('some.url');
    $scope.form.input1 = 'input1';
    $scope.form.input2 = 'input2';

    $scope.submitForm = function() {
        document.getElementById('appForm').submit();
    };
    $timeout(function() { $scope.submitForm() });
}
Sign up to request clarification or add additional context in comments.

1 Comment

The interpolated value in the action attribute will be evaluated and filled in after a digest cycle after this controller gets initialized.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.