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

Exploring AngularJS: Effortlessly Retrieving Object Values

HTML: <div ng-app = "" ng-controller = "personController"> <p>Persoon: <select ng-model="persoon"> <option ng-repeat="person in persons">{{person.FirstName}} {{person.FamilyName}}</option> & ...

Creating interactive tabs within the HTML document with AngularJS

Trying to customize the tab layout on my HTML page and I have a code similar to this: <form name="requestForm"> <div class="form-group col-md-6 md-padding"> <div class="text-primary"> <h3>Create Request</h3> ...

utilizing Nuxt code in Elixir/Phoenix

Overview In my previous work, I combined frontend development with nuxt and backend support from elixir/phoenix, along with nginx for reverse proxy. Looking to enhance the performance of the system, my goal is now to migrate everything to Elixir/Phoenix. ...

JavaScript fails to effectively validate URLs

I have implemented the following validation for URLs: jQuery.validator.addMethod("urlValidatorJS", function (value, element) { var regExp = (/^HTTP|HTTP|http(s)?:\/\/(www\.)?[A-Za-z0-9]+([\-\.]{1}[A-Za-z0-9]+)*\.[A-Za-z]{ ...

having difficulty inputting time into a time controller with selenium webdriver

This is the HTML Code: <mat-form-field _ngcontent-c18="" class="mat-input-container mat-form-field ng-tns-c7-23 mat-form-field-type-mat-input mat-form-field-can-float mat-form-field-should-float mat-primary ng-untouched ng-pristine ng-invalid"><d ...

Is it possible to utilize the useRef Hook for the purpose of storing and accessing previous state values?

If you have already implemented the useState and useEffect Hooks for maintaining previous state, another approach is to utilize the useRef Hook to track previous state values as well. ...

The code provided creates a web form using HTML and ExpressJS to store submitted information into a MongoDB database. However, there seems to be an issue with the 'post' method, while the 'get' method functions correctly

When I try to submit entries (name, email, address) on localhost:3000 in the browser, the 'post' function is not creating an object in the mongo database even though it works from the postman. The browser displays an error message saying "unable ...

Regular Expression designed specifically for detecting alternative clicks

When using ngpattern for validation, I have encountered an issue where my error message is displaying for a different reason than intended. The error message should only show if the field is empty or contains only special characters. <textarea name="ti ...

How come .trim() isn't cooperating with me?

I am encountering an issue with this particular piece of javascript. Every time I attempt to use it, nothing is displayed in my div. Instead, it simply adds ?weight=NumberInputed&measure=lbsOrkgs&submit=Submit to the URL. <h2>What size d ...

Unable to resolve the issue of Duplicate keys detected with value '0'. This could potentially lead to errors during updates

Encountered a warning in Vue js stating 'Duplicate keys detected: '0'. This warning could potentially lead to an update error. To resolve this issue, I utilized the getter and setter in the computed variable and dispatched the value to Vuex ...

Where can I find the JavaScript code that controls the button function?

Is there a method to identify the trigger that activates a button's specific action and page refresh, such as this example: <input type="submit" name="name" value="some value" id="mt1_main_btn" class="btn_next"> Simply copying the button does ...

Using HTML, CSS, and JavaScript, the main tab must include nested subtabs to enhance navigation and

When a user clicks on a tab, another tab should open within the main tab. Depending on the selection in the second tab, input fields should appear while others hide. Something similar to the nested tabs on expedia.com. I have experimented with the tab vie ...

Using jQuery to fetch and read the source code of a specified URL

I'm facing an issue with extracting the source code of a website URL into a variable. Here is my current approach: <script type="text/javascript"> debugger; $(documnet).ready(function () { var timer = $.ajax({ type: ...

Utilize Haxe Macros to swap out the term "function" with "async function."

When I convert haxe to JavaScript, I need to make its methods asynchronous. Here is the original Haxe code: @:expose class Main implements IAsync { static function main() { trace("test"); } static function testAwait() { ...

Ways to enhance the functionality of an input component

Within my React app, I have an input component that triggers an onChange event when connected to another component: <input type="text" onChange={onChange} /> Now, I am looking to enhance this input component by incorporating a prop from another com ...

Tips for turning off the auto save password option on browsers for user registration forms

Is there a way to prevent browsers from saving passwords on the add users form? I've tried using autocomplete="off" but it doesn't seem to work for saved passwords. I've searched extensively for a solution but haven't found the right on ...

What are the steps to restrict the scope of a <style> declaration in HTML?

Currently, I am in the process of creating a user interface for a webmail. Whenever I receive an email, I extract its content and place it within a <div> element for display purposes. However, one challenge that I am facing is that each email contain ...

Having trouble accessing object values in ReactJS

Currently, I am having an issue passing a value into the redux store through reducer. Despite successfully displaying this value in the component, I encounter an error stating "cannot read property name of undefined." The situation gets even more puzzling ...

Encountering unanticipated undefined elements while incorporating map in JSX

I'm attempting to dynamically generate <Audio> components using the map method: import React, { useState, useRef, createRef } from 'react'; const audios = [{ src: '/fire.mp3' }, { src: '/crickets.mp3' }]; function ...

What is the best way to delete an added element once a DIV has been toggled with JQuery?

I'm facing an issue where I need to add an element to a DIV that has a toggle function. However, every time I click the toggle again after adding the element, the same element is appended once more, resulting in duplicate elements. Upon observation, ...