Developing dynamic objects for input string fields in AngularJS

In my AngularJS view, I have the following setup:

<label class="control-label">Name:</label>
  <input type="text" class="form-control" ng-model="config.name" />
<br />

<label class="control-label">versionSpecificApiConfig:</label>
<input type="text" class="form-control" ng-model="config.versionSpecificApiConfig" />
<br />

<label class="control-label">schemaUrl:</label>
<input type="text" class="form-control" ng-model="config.versionSpecificApiConfig.schemaUrl" />
<br />

I'm setting this up to achieve a specific structure for the config object:

{
 "name":"string",
 "versionSpecificApiConfig":{
  "string":{
           "schemaUrl":"string"
           }
      }
  }

However, I encountered an error stating "cannot set property for string in versionSpecificApiConfig field". Can someone assist me in creating a structure like this? (The use of "string" in the structure refers to the respective values of the textboxes).

Answer №1

Are you interested in a solution like this?

let application = angular.module("application" , []);
application.controller('Controller', ['$scope', Controller]);
function Controller($scope){
 $scope.configuration = {};
 $scope.displayInfo = function(){
     console.log($scope.configuration);
 }
};
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.14/angular.min.js"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<div ng-controller="Controller" ng-app="application">
<label class="control-label">Name:</label>
  <input type="text" class="form-control" ng-model="configuration.name" />
<br />

<label class="control-label">versionSpecificApiConfig:</label>
<input type="text" class="form-control" ng-model="configuration.versionSpecificApiConfig.string.schemaUrl" />
<br />

<label class="control-label">schemaUrl:</label>
<input type="text" class="form-control" ng-model="configuration.versionSpecificApiConfig.string.schemaUrl1" />
<br />

<button type="button" ng-click="displayInfo()">Click to see object</button>
<br/>
{{configuration}}
</div>

Answer №2

I have implemented a solution for this issue and it is working perfectly. Give it a try and let us know.

<form ng-submit="func(config)">
        <label class="control-label">Name:</label>
          <input type="text" class="form-control" ng-model="config.name" />
        <br />

        <label class="control-label">versionSpecificApiConfig:</label>
          <input type="text" class="form-control" ng-model="config.versionSpecificApiConfig" />
        <br />

        <label class="control-label">schemaUrl:</label>
          <input type="text" class="form-control" ng-model="config.schemaUrl" />
        <br />

        <button type="submit"> Save </button>
      </form>

$scope.func = function(Obj){
      var keyObj = {};
      var versionObj = {};
      var data = {};

      var key = Obj.versionSpecificApiConfig;

      keyObj['schemaUrl'] = Obj.schemaUrl;

      versionObj[Obj.versionSpecificApiConfig] = keyObj;

      data['name'] = Obj.name;
      data['versionSpecificApiConfig'] = versionObj;
      console.log(data);
      var Json = JSON.stringify(data);
      console.log(Json);
    }

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 trigger two functions simultaneously using an onClick event in NextJS?

I have defined two constants in my NextJs app, "toggleMenu" and "changeLanguage". When the language changer inside the menu is clicked, I want it to trigger both of these functions. However, I tried implementing this code but it doesn't seem to work ...

Issue with AngularJS date validation occurring only on the initial attempt

When I use the input type "date" tag, I expect it to validate the date. For example, April cannot have 31 days and so on. However, this validation does not work the first time I set the date as "31-04-2014". If I go back to the date field and change the ...

Troubleshooting TypeScript in Firefox

When it comes to running my Angular 2 application using Node and generating .map files, everything seems to be going smoothly. I also use systemjs. However, I have encountered an issue when trying to debug .ts files in Firefox. The browser reports: "Not f ...

What is the function of async in Next.js when triggered by an onClick

Need help with calling an async function pushData() from a button onClick event async function pushData() { alert("wee"); console.log("pushing data"); try { await query(` //SQL CODE `); console.log("Done&quo ...

Revisiting the Issue: Jquery Hover Effect Fails to Function in Internet Explorer

Can anyone provide assistance with this issue? The code snippet works perfectly in all browsers except for IE, where the menu does not fade in and looks unattractive. html <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.o ...

"Utilizing AJAX to dynamically extract data from PHP and manipulate it in a multi-dimensional

Just starting out with JSON/AJAX and I'm in need of some help... I have a PHP page that seems to be returning [{"id":"1"},{"id":2}] to my javascript. How can I convert this data into something more user-friendly, like a dropdown menu in HTML? Here i ...

What are the steps to view my HTML webpage on a smartphone?

Recently, I successfully created an HTML webpage with CSS and JS that looks and functions perfectly on my PC. However, when attempting to access it on my phone, I encountered some issues. Despite transferring all the necessary files to my phone, only the ...

Updating entire DOM in javascript

Implementing undo-redo functionality in my project has proven to be quite complex, as every change affects numerous elements. I believe that saving and restoring the entire page may be the most effective solution. However, I have encountered issues with mi ...

Clicking on the button will allow you to sort the items in

I need assistance with sorting a table: when I click on a header, the order changes to ascending but does not toggle back to descending. Despite having a boolean value that should switch between true and false, it keeps returning the initial value. Can s ...

Ensure the smooth scrolling feature is activated by adding an active class when either clicking or manually scrolling the

I have a script that enables smooth page scrolling, but I want it to automatically add an "active" class to the link corresponding to the section currently in view. While there are similar solutions out there, most of them only apply the class when the lin ...

Can you explain the purpose of the window.constructor and global.constructor functions in JavaScript?

Can someone explain the purpose of this function? I've been searching for information but can't find anything. I tested it in Firefox: window.constructor() // TypeError: Illegal constructor new window.constructor() // TypeError: Illegal constru ...

Rendering an element in React Router Dom based on specific conditions

Starting a new project with the latest version of react-router-dom and following their recommendation to use createBrowserRouter. The goal is to display a different header based on the path parameters. Currently, I have set up an optional path parameter: ...

Issue with jQuery hide() function in Internet Explorer 9

I need help with creating a hidden div that becomes visible when a user clicks a link. The code I have works in FF / IE7 / IE8 but not in IE9, where the div is always visible without any content. Any advice is appreciated! <script> $(document).r ...

Tips for saving an image that has been dragged onto the browser locally

I recently started working with Angular and decided to use the angular-file-upload project from GitHub here. I'm currently in the process of setting up the backend for my application, but I'd like to be able to display dropped files locally in th ...

Managing the re-rendering in React

I am encountering a situation similar to the one found in the sandbox example. https://codesandbox.io/s/react-typescript-fs0em My goal is to have Table.tsx act as the base component, with the App component serving as a wrapper. The JSX is being returned ...

Is a single f.select impacting another f.select in the same form? (undesired)

I am facing an issue where adding a new HTML element like: <%= f.date_select :date, { id: "date-select"} %> is impacting my existing collection select: <%= f.collection_select :id, Customer.where(business_id: current_c ...

Developing an interactive selector within the on() event handler

My goal is to utilize the on() event for managing dynamically created code. It functions properly when the selector is hardcoded in the on() event. However, I aim to enable it to select different elements depending on which box they choose. $("body").on( ...

Is it feasible to utilize the .fadeTo() function in jQuery multiple times?

I need some help with writing a script that will display a warning message in a div tag every time a specific button is pressed. I chose to use the fadeTo method because my div tag is within a table and I want to avoid it collapsing. However, I'm noti ...

Combining arrays of objects in JavaScript

I am currently working on combining different arrays: const info1 = {id: 1} const info2 = {id: 2} const info3 = {id: 3} const array1 = [info1, info2] const array2 = [info1, info3] const array3 = [info2, info3] const union = [...new Set([...array1, ...arr ...

Tips for safely storing JWT in the browser

I am currently working with Angular 10 on the front-end and obtaining a JWT from backend services. I am seeking the best method to securely store my Okta JWT in the browser while also mitigating the risk of XSS and XSRF attacks. I have looked into storing ...