Generating hidden form fields dynamically with AngularJS

In the midst of developing an AngularJS application, I'm faced with a requirement to pass hidden variables to a third-party application. These variables need to be fetched from a database.

To accomplish this, I've implemented the following code snippet to dynamically generate hidden variables:

<input type="hidden" ng-repeat="hdnvar in models.MyModel.templateVariables" name="{{hdnvar.Key}}" id="{{hdnvar.Key}}" value="{{hdnvar.Value}}" />

Upon clicking the submit button, the following function is triggered:

$scope.getDetailsForTP = function () {
        $scope.models.MyModel.templateVariables = {};
        $http({
            url: "http://localhost:11149/MyService.svc/TemplateVariable",
            dataType: "json",
            headers: {
                'Content-Type': 'application/json; charset=utf-8'
            }
        }).then(function successCallback(response) {
            if (response.status == 200) {
                $scope.models.MyModel.templateVariables = response.data;
                $scope.submitForm();
            }
            else {
                alert('Error occurred in fetching template variable data');
            }
        }, function errorCallback(response) {
            //do something
        });
    };
    
$scope.submitForm = function () {
    document.getElementById("apirequest").submit();
};

While the hidden variables appear correctly on the page, I noticed that they do not get submitted when reviewing Fiddler. Any assistance on this issue would be greatly appreciated.

Answer №1

Once you have updated the templateVariables, make sure to submit the form with a slight delay to allow for the rendering of HTML elements.

To achieve this, use the following code snippet:

$scope.models.MyModel.templateVariables = response.data;
$timeout($scope.submitForm, 1000)  // Submit the form after a 1 second delay

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Improving React's onClick Functionality for Better Performance and Reducing Re-render

Can the method of setting the isVisible state impact performance when dealing with two different divs? Specifically, does the ShowDiv approach recreate the function on each render? Is there any advantage to using ShowDiv2 over ShowDiv, or are they essenti ...

Encountering the error message "handleChange is not a function" when trying to select a date in Material UI

Encountering an error message 'handleChange is not a function' when selecting a specific date in the DatePicker component. The DatePicker component is nested within the Controller component of react-hook-form. The expected behavior is to display ...

Using jQuery to highlight the navigation menu when a specific div scrolls into view

I have implemented a side navigation consisting of circular divs. Clicking on one scrolls you to the corresponding .block div, and everything functions correctly. However, I am now curious if it is feasible to highlight the relevant .nav-item div based on ...

Executing a Cron Job several times daily, each and every day

This is my current code snippet: const CronJob = require('cron').CronJob; new CronJob({ cursoronTime: '* * * * *', // every minute onTick: async function() { console.log(&ap ...

What is the best way to incorporate the compiled output of a JavaScript ES6 repository into another repository

I have two GitHub JavaScript repositories: "chromeextension" and "core". I would like the "chromeextension" repository to consume the build output of the "core" repository (as the "core" repository is written in ES6 and needs to be compiled to ES5 for use ...

What is the best way to integrate an array from an external JavaScript file into a Vue.js component?

I am struggling to import an array into a Vue component: This is my simplified component: <script type="text/babel"> const codes = require('./codes.js'); export default { props: [], data() { return { ...

sound not functioning on iPad

<audio id="example" controls="controls"> <source src="1.mp3" type="audio/mpeg" /> <source src="1.ogg" type="audio/ogg" /> </audio> JavaScript code to automatically play video when the page loads document.getElementById(&ap ...

Beware of UTF-8 Decoding Problems: Avoid using "0"-prefixed octal literals and octal escape sequences as they are outdated. For octal literals, opt for the "0o" prefix

I've hit a roadblock trying to achieve this task, any assistance would be greatly appreciated. I have a string that looks like this "jas\303\241nek" and I need to convert it to look like "jasánek". After using [this web ...

Images in Android webview disappearing after first load

When it comes to loading a local HTML file with local images into a WebView on Android, everything seems to work fine on emulators and newer devices. However, I encountered an issue with a much older device running Android 2.3.4. Initially, the images disp ...

The length of the JavaScript array is not accurate

In my code, there is an array named 'plotData' which contains multiple 'rows' of data, each represented by a 4-element array. The array 'plotData' gets updated by a $.post() function further down in the script. The placeholder ...

Examining REST API functionality through the use of a Console, requiring an integer list as a parameter

Currently, I am in the process of testing a REST API to perform an action that requires a list of integers. I am uncertain about how to correctly handle the parameters required for this test. In my request payload, I have included the following: idAttac ...

What is the best way to use JavaScript to show a text value alongside radio buttons?

Currently, I am in the process of creating an e-commerce platform that allows customers to choose custom components for their purchases. Although I am relatively new to JavaScript, I have successfully developed a radio button list where the prices are tota ...

What causes the difference between object[key] and Object.key in JavaScript?

After running the following code snippet, I observed that "typeof object[key]" is displaying as a number while "typeof object.key" is showing undefined. Can anyone explain why this unusual behavior is occurring? var object = {a:3,b:4}; for (var key in o ...

What could be causing the JavaScript alert to trigger twice?

<script type="text/javascript"> function ChangeStyle() { document.getElementById("p1").innerHTML = "<a href='javascript:void()' onclick=\"window.location.href='http://google.com'\">The New Link</a&g ...

Transfer a JSON object to a Java Class without relying on a servlet

I have a form in HTML where I collect user input and store it as an object in JavaScript. Here is how I am creating the object: var dataObject = { Name: getName(), Age : getAge() } Now, I want to send this object using Ajax to a b ...

Deactivate the submit button when the form is not valid in angularjs

I am currently facing a challenge with my form that contains multiple input fields. To simplify, let's consider an example with just two inputs below. My goal is to have the submit button disabled until all required inputs are filled out. Here is wha ...

JavaScript: Modify the dropdown class with a toggle option

Hi there, I'm currently facing a small issue and I could really use some assistance. I have a dropdown menu with two selection options - "green" and "blue", and I need to toggle a class based on the selected option. If you'd like to take a look, ...

Securing a route using a referrer in Node.js: Best practices

Within my node.js application, I am looking to secure a specific route so that users can only access the page /post if they are coming from /blog. If the user accesses the route from any other source, they should be redirected to /. I have implemented the ...

Javascript Flickering Effect in HTML5

Currently, I am in the process of creating a simple game using javascript without jQuery. However, I am facing an issue with flickering on the canvas due to the clearing command. After researching solutions online, I came across suggestions for implementin ...

I have a Key in my component, but it is still searching for a unique key

Just diving into React JS and attempting to transfer data from one component to another using the Link to method I found online... Error: index.js:1 Warning: Each child in a list should have a unique "key" prop. Checking the render method of Videos. Refe ...