ngModel is not taken into account when processing form data

Attempting to make use of a dynamic form in AngularJS, the code snippet below has been utilized:

<dynamic-form 
template="formTemplate"
        ng-model="formData"
        ng-submit="processForm()">
</dynamic-form>

The controller script includes the following lines:

$scope.formData = {}; // A JavaScript object is needed to store the form's models.

$scope.formTemplate = [];

The 'onsuccess' function looks like this: console.log('User registration form :'+ response); // The form-id will be transformed to a string // var form-id = response.form-id; var result = toArray(response); $scope.formTemplate = result;

}

function toArray(obj) {
  var result = [];
  var model = [];var type = [];var label = [];
  for (var prop in obj) {
    var value = obj[prop];
    angular.forEach(obj.fields, function(val,key){
        if(val.field_type != ''){type.push(val.field_type);}
        else{type.push(0);} 
        model.push(key);
    });
  }
  for(var j=0;j<model.length;j++){
    if(type[j] === 'textfield' || type[j] === 'password'){
        type[j] = 'text';
    }
    else if(type[j] === 'email'){
        type[j] = 'email';
    }
   else{
      type[j] = type[j];
   }

    console.log("result.. " + model[j] + "......"+ type[j]);
     result.push(
       {
           "type": type[j],
           "label": model[j],
           "model": model[j]
       }
     );
  }
  console.log("Result..." + result);
  return result;
}

This dynamic form library is being used: https://github.com/danhunsaker/angular-dynamic-forms

However, while the given example in the library works fine, the scenario above does not. Any ideas on what might be causing the issue? Is it related to model evaluation?

The sample response provided by me is as follows:

 var response =        {  
   "form-id":"user_register_form",
   "fields":{  
      "name":{  
         "field_type":"textfield",
         "description":""
      },
      
      "mail":{  
         "field_type":"textfield",
         "description":""
      },
      "field_first_name":{  
         "field_type":"textfield",
         "description":""
      },
     "field_gender":{  
         "field_type":"radio",
         "options":{  
            "male":"Male",
            "female":"Female"
         },
         "description":""
      }
     
   }
}

Answer №1

It seems like the issue lies in the fact that your result variable is holding a $resource object instead of a regular object. You can resolve this by making the following changes:

function successHandler(response) {
  var result = convertToArray(response);
  $scope.templateForm = angular.toJson(result);
}

Answer №2

For those who come across this now, the issue lies with the library being unable to handle form re-rendering when the template changes. It can be quite challenging to manage all the aspects involved in re-rendering a form, especially when it already contains data, and I haven't fully grasped all the requirements.

I would advise steering clear of the library I was using and opt for one of the alternatives listed in its README. These alternative libraries are generally more reliable and regularly updated.

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

What is the best way to pass value to a URL in a React JS application?

Is there a way to properly use history.push in React to push text to the URL route manually? I am trying to achieve this in my onChange method for an input field: function Header(props) { return ( <div> <input type="radio" onChan ...

`Write a custom AngularJS directive`

I followed the official AngularJS documentation to implement this directive. However, I am facing an issue with setting validity using the $setValidity() method in the link function of my directive. The changes are not reflecting in the view when I try to ...

What is the best way to transfer information from an old JSON file to a new JSON file using JavaScript

I have a JSON file and I need to extract the content of "data" where the provider is "AXIS DATA" into a new JSON file. How can this be achieved? Here's what I've attempted: First, I convert it using JSON.parse and then search for the desired da ...

What is the reason behind div elements shifting when hovering over a particular element?

Currently, I have floated all my div elements (icons) to the left and margin-lefted them to create space in between. I've displayed them inline as well. However, when I hover over one element (icon), the rest of the elements move. Can you please help ...

Error: NextJS cannot access the 'map' property of an undefined object

Hey everyone, I usually don't ask for help here but I'm really struggling with this piece of code. I've tried looking at various resources online but can't seem to figure out why it's not working. Any guidance would be greatly appr ...

Executing functions during the page loading process

I'm currently in the process of integrating a new payment method into a checkout page that does not have built-in support for it. The button on the page that redirects to an external payment portal requires specific parameters to be passed. I've ...

Tips for utilizing a button within a hyperlink in a React component:

I am new to react and have been working on the following code: import {NavLink as Link} from 'react-router-dom' return ( <div> {posts.map((actualData, index) => ( <Link to={'/' + actualData.id}> <d ...

Unexpected behavior when using Async.map in conjunction with async.waterfall

Utilizing Async, I am using async.map() to connect my data array with a function and incorporating async.waterfall() within that function to execute functions in a series. However, the waterfall function is not functioning as anticipated. I have also attem ...

Quick tip on closing an Angular-ui modal from a separate controller

I am currently using Angular-ui to display a modal with a form inside. Here is the code snippet: app.controller('NewCaseModalCtrl', ['$http', '$scope','$modal', function ($http, $scope, $modal, $log) { $scope.ite ...

The PHP AJAX call is returning an undefined response status

I'm facing two issues here. The first problem is that when I try to access response.status, it returns undefined. The second issue is related to the creation of "$_SESSION['promo-code'] = $arr". When I enter a promo code, I encounter the fol ...

Excessive iterations occurring in JavaScript for loop while traversing an array

After addressing the issues raised in my previous inquiry, I have made significant progress on my project and it is now functioning almost exactly as intended. The main purpose of this website is to iterate through the World Of Tanks API to generate cards ...

Display a division in C# MVC 4 when a boolean value is true by using @Html.DropDownList

I have multiple divs stacked on top of each other, and I want another div to appear when a certain value is selected. I'm familiar with using JavaScript for this task, but how can I achieve it using Razor? Below is a snippet of my code: <div id=" ...

Adding a uniform header and footer across all pages simultaneously in HTML

I am currently working on a project where I want to apply the same header and footer design from my homepage to all pages. However, I need a method that allows me to update the header and footer in one place, so that any changes I make will automatically r ...

Trouble with Displaying 3rd Level JQuery Dropdown Menu

Currently working on implementing a 3 level dropdown feature. I have been using a third-party responsive menu in Opencart and it's been working well. You can see a demo of it here: Unfortunately, Opencart does not natively support 3 level categories, ...

Is it possible to call a REST API in Javascript by passing the username and password as

I have been attempting to use Javascript to call the AwaazDe REST API with a specific username and password, but I'm encountering issues. Below is my code: function authenticateUser(user, password) { var token = user + ":" + password; ...

Learn how to automatically update data in Vue JS after making a 'POST,' 'DELETE,' or 'PUT' request in Vue JS 2

Whenever I send a 'POST' or 'DELETE' request, the data doesn't update automatically. I have to manually reload the page to see the updated information. Is there an easy way to refresh the data after making any 'POST' / &a ...

Is it possible to utilize Angular validation directives programmatically within a personalized directive?

In my exploration of HTML inputs, I have noticed a recurring pattern specifically for phone numbers: <input type="text" ng-model="CellPhoneNumber" required ng-pattern="/^[0-9]+$/" ng-minlength="10" /> I am interested in developing a unique directiv ...

What is the quickest way to accomplish this task?

Currently, I am in the process of constructing a dashboard using ASP.Net MVC, Angular.js, SQL Server, and Fusion charts. The data needed for charting is stored in a database and obtained through a stored procedure. My objective now is to convert the result ...

Can you assist me with setting a value in an ASP dropdownlist?

I have an asp dropdownlist and I need to set its value from the client side. Although I am able to retrieve the data from the client side, I am facing difficulty in setting it in my asp dropdownlist using JavaScript. HTML <div class="col-md-6 form-gro ...

What's the best method for concealing components: using a class to hide or removing them from the

I am encountering difficulties in React as I try to add a class to a component after a specific time duration. My goal is to apply the "hidden" class to a loading image, changing it to display: none after a few seconds. However, despite my efforts, I keep ...