CodexBloom - Programming Q&A Platform

How to bind a dynamic input field to an AngularJS scope with two-way data binding using ng-repeat?

πŸ‘€ Views: 41 πŸ’¬ Answers: 1 πŸ“… Created: 2025-06-06
AngularJS ng-repeat data-binding JavaScript

I've tried everything I can think of but I've encountered a strange issue with I'm relatively new to this, so bear with me. I'm stuck on something that should probably be simple... I'm currently working on an AngularJS application (version 1.8.2) where I need to create a dynamic list of input fields, each bound to its respective model property on the scope. I want to achieve two-way data binding so that any changes in the input field reflect on the model object and vice versa. I've tried using `ng-repeat` to loop through an array of objects, each containing a unique `inputValue` property. However, I'm working with issues where the input fields do not update the model as expected, or they overwrite each other's values. Here’s the code snippet I’m working with: ```html <div ng-app="myApp" ng-controller="MyController"> <div ng-repeat="item in items"> <input type="text" ng-model="item.inputValue" /> </div> <button ng-click="addItem()">Add Item</button> <pre>{{ items | json }}</pre> </div> ``` And in my controller, I have: ```javascript angular.module('myApp', []).controller('MyController', function($scope) { $scope.items = [{ inputValue: '' }]; $scope.addItem = function() { $scope.items.push({ inputValue: '' }); }; }); ``` When I input data into one of the fields, it appears to be updating the model correctly at first, but upon adding a new item, the new input fields do not maintain unique bindings. Instead, they seem to share the same model reference, resulting in unexpected behavior where changing one input also changes another. This is leading to a frustrating experience for the users. I’ve also checked for debug messages in the console, but nothing seems to indicate a question. Is there something I'm missing in how I'm structuring the data-binding or how I’m using `ng-repeat`? Any insights on improving this structure would be greatly appreciated! This is part of a larger service I'm building. I'm working with Javascript in a Docker container on Ubuntu 20.04. Could someone point me to the right documentation? The project is a web app built with Javascript. Thanks, I really appreciate it! This issue appeared after updating to Javascript stable. What am I doing wrong?