Updating ng-model with the dynamic value of a JavaScript variable in an AngularJS environment

I have encountered similar questions in the past and have tried the solutions provided, but my issue has a unique twist.

Imagine I have an array of objects like this in my controller:

$scope.objArray = [
  {
    id:1,
    name:"placeholder1"
  },
  {
    id:2,
    name:"placeholder2"
  }
];

Based on this array, I am dynamically generating a form like this:

<div ng-repeat="field in fields">
    {{field.field_name}} </br>
    <input type="text" ng-model="field.field_name"><br>
</div>

In this form, I want the ng-model value to be set as placeholder1 and placeholder2, not as a JavaScript variable like field.field_name. This is because my targets are sourced from another location containing HTML code like the following:

<p>this is sample html {{placeholder1}}. again sample html {{placeholder2}} </p>

I do not have access to those JS variables here. So far, I have not found a solution that caters to my specific requirements.

Here is a link to the Plunker showcasing this issue: http://plnkr.co/edit/ttaq0l3PDRu4piwSluUX?p=preview

Answer №1

For your specific scenario, the code will appear as follows:

<div ng-repeat="item in items">
    {{item.item_name}}
    <br>
    <input type="text" ng-model="$parent[item.item_name]">
    <br>
</div>

It is important to note that the $parent reference must be used because you are aiming to modify a property in the outer scope (where objectList is created), but since ngRepeat generates individual child scopes for each iteration, you need to move one level up.

Example: http://example.com

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

Is the AngularJS application failing to transmit data accurately to Node.js?

I've been grappling with this issue for a few days now and I can't seem to pinpoint the problem. As someone relatively new to the MEAN stack, I might be overlooking something obvious. I followed the boilerplate code from mean.io for both the back ...

Responsive Design: Concealing a Sidebar with CSS

Seeking assistance with optimizing the layout of my app, . It currently displays a menu on the left and a login form in the center, with a graph shown to users upon logging in. Is there a way to configure it so that when accessed on iOS: 1) the left menu ...

Devextreme experiences the submission of multiple forms simultaneously

While working with Devextreme forms in Angular 2, I encountered an issue with two separate forms. Whenever I click the submit button on either the first or second form, both forms undergo validation. Why is this happening? https://i.sstatic.net/1orrY.png ...

Add a decorator to all functions in a TypeScript class to list all available methods

How can I apply a decorator function to all methods within a class in order to streamline the code like this: class User { @log delete() {} @log create() {} @log update() {} } and have it transformed into: @log class User { ...

Modify a single field in user information using MySQL and NodeJS

I am currently working on developing a traditional CRUD (create, read, update, delete) API using NodeJS/Express and MySQL. One of the routes I have created is responsible for updating user information, which is functioning correctly. The issue: When I d ...

Retrieve an array of objects by matching a specific object id

Seeking assistance for a React project centered around a quiz application. Within my database (currently utilizing Firebase Realtime Database), I have a collection of questions and another collection of answers. Questions are identified by an Id, title, an ...

Error message displayed: "The timer function encountered an issue as second_input is found to be null."

var idinterval; var second_input_value = document.getElementById("Seconds"); var minute_input_value = document.getElementById("Minutes"); var second_val = second_input_value.value; var minute_val = minute_input_value.value; var total_time = parseInt(minut ...

Phaser 3 shows images as vibrant green squares

In my project, I have two scenes: Loading and Menu. In the loading scene, I load images with the intention of displaying them in the menu. Here is the code for the Loading scene: import { CTS } from './../CTS.js'; import { MenuScene } from &apo ...

I'm searching for a universal guidebook on creating web page layouts

After 5 years of creating webpages, I have realized that my sites tend to have a nostalgic 1995 internet vibe. Despite being a C# programmer with knowledge in HTML, JavaScript, and CSS, my design skills could use some improvement. Is there a quick referenc ...

Node.js and Jest resolve module paths in varying sequences

Within my Node.js project, I have the passport package installed alongside my custom config/passport.js file containing Passport strategies and app configuration in config/config.js. To streamline my workflow on Ubuntu 16.04, I configured my NODE_PATH to ...

Assigning a value to a JavaScript variable using Ajax

Struggling with a problem for hours and still can't find the issue... A registration form is set up for users to create accounts. When the submit button is clicked, a validateForm function is triggered. Within this function, some JavaScript tests ar ...

Set the android camera resolution to a lower setting automatically when a user attempts to upload a file from their browser

In my current application, I have implemented a feature that allows users to upload files. When the user clicks on the input field, they are presented with options to either select a video from their gallery or open the camera to record a new video and u ...

The state in Redux Toolkit seems to be stuck and unchanging

After receiving my data back in JSON format and successfully logging it using console.log(), I'm facing an issue with changing the state. Can anyone provide some insights on why this might be happening? Please bear in mind that I am still in the learn ...

What is the best way to use jQuery's $get() function to import a text file from an external website into a JavaScript

Once the button is clicked within the script below, it will load the contents of the file "" and present it on the screen. How should the .get() function syntax be correctly written to fetch data from an external website and place it in #content? <!DO ...

Incorporating HTML5 transitions into your website

After researching HTML5 transitions and adapting some examples, I finally achieved the desired effect. <!DOCTYPE html> <html> <head> <style> div { width: 50px; height: 50px; } div. ...

Unexpected JavaScript Bug (with jQuery)

I have an interesting code snippet that captures button clicks and then hides the existing content, revealing the content of the clicked item instead. This code is part of my init.js file along with other functionalities: $(function() { $('#conta ...

Angular.js is experiencing difficulties when using the input value attribute in conjunction with ng-model

I've been hard at work on an app that allows users to edit items, with those changes updating in a database. To prevent empty form submissions, I automatically fill the input fields with the current item's information. form.form-update(method="p ...

Can you tell me the specific name of the HTML document style that features two columns?

Here are two websites that showcase a unique two-column style layout: http://momentjs.com/docs/ I find these sites visually appealing and I am curious about the specific name of this style. Is there a template or tool available to create documents with a ...

Retrieving user information using the ng-click directive

Here is a list of users that I want to showcase. I aim to reveal specific user details upon clicking on him or her. <head> <script> (function() { angular.module("testApp", ['ui.bootstrap']).controller('testCtrl&ap ...

What is the best way to input information into my Google spreadsheet with React JS?

After using https://github.com/ruucm/react-google-sheets as a reference, I encountered a persistent gapi 404 error whenever I tried to run the code. It seems like the GitHub link might not exist, causing my app to fail. However, I could be mistaken. Is t ...