What is the best way to implement ng-model in an ng-repeat loop within AngularJS?

Can ng-model be utilized inside an ng-repeat loop in AngularJS? My database is mySQL and I am attempting to do this so that I can edit data on the same row where it is displayed. Hopefully, this explanation makes sense.

<tbody ng-repeat="x in names">
    <tr class="edit-row">
        <td>
            <div class="form-group editable">
                <div class="input-group">
                    <input class="form-control" name="x.fname" ng-model="x.fname_m" value="{{x.fname}}" disabled /> </div>
            </div>
        </td>
        <td>
            <div class="form-group editable">
                <div class="input-group">
                    <input class="form-control" value="{{x.email}}" disabled /> </div>
            </div>
        </td>
        <td>
            <button class="btn btn-xs btn-default btn-collapse" data-toggle="collapse" data-target="#followUps1"><i class="fa fa-calendar"></i> </button>
            <a class="btn btn-xs btn-warning btn-collapse" data-toggle="collapse" data-target="#oppTasks1"> <i class="fa fa-star"></i> </a>
            <a class="btn btn-xs btn-default btn-collapse" data-toggle="collapse" data-target="#viewDetail1"> <i class="fa fa-eye"></i> </a>
            <a class="btn btn-xs btn-default btn-collapse" data-toggle="collapse" data-target="#viewComment1"> <i class="fa fa-comments"></i> </a>
            <button ng-click="updateData(x.id, x.fname, x.lname, x.email, x.phone,  x.country, x.skype_id, x.lead_source, x.lead_status, x.lead_priority)" class="btn btn-edit btn-primary btn-xs"><i class="fa fa-pencil"></i>
            </button>
            <button class="btn btn-update btn-success btn-xs"><i class="fa fa-floppy-o"></i>
            </button>
        </td>
    </tr>
</tbody>

Answer №1

Here is a possible solution:

<tbody ng-repeat="item in items track by $index">
  <tr class="edit-row">
    <td>
      <div class="form-group editable">
        <div class="input-group">
          <input class="form-control"
            name="item.name" ng-model="item.name_m[$index]"
             value="{{item.name}}" disabled /> </div>
        </div>
      </td>
//... more code can go here

Make sure to use 'track by $index' in your ng-repeat, and access the model using model[$index]. It's also a good idea to initialize the model to null at the beginning of your controller if it's empty:

$onInit() {
  this.name_m = {};
}

Answer №2

<div ng-repeat="element in elements">
<input ng-model="element.data">
</div>

or connects to

<div ng-repeat="element in elements">
{{ element.dataset }}
</div>

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

An error will occur if you try to modify the state of a component from outside the component

Creating a modal component that triggers a bootstrap modal from any section of the application and then defines custom states for that component externally. It is functional, however, an error message consistently appears upon opening the modal, and I am u ...

Pause before proceeding to the next iteration in the loop

I am currently facing an issue while trying to send a message through an API using a function. The function returns a value called messageLogId, which I'm attempting to update in the main loop at Attendence. However, when executing the code, the value ...

Guide on filling in credentials in Facebook popup using Webdriver with Javascript

On my website, I have a feature where users can authenticate using Facebook. Currently, my site is not public, but you can see a similar process in action at agar.io. Just click on "Login and play" and then click on "Sign in with Facebook". This will open ...

What is the best way to integrate Google Maps into a Vue component?

I found a helpful reference on how to retrieve the formatted address from a dragged marker in Google Maps using JavaScript in this Stack Overflow thread. Now, I am looking to implement this functionality in a Vue component. I attempted to do it like this ...

Exploring Shader Materials and OpenGL Framebuffers within the realm of THREE.js

In my current project, I am attempting to utilize an FBO within a material in THREE.js. The project involves a GPU-based fluid simulation that generates its final visualization to a framebuffer object, which I intend to use as a texture for a mesh. Below i ...

What is the best way to integrate passport with the existing bcrypt code in my project?

I've been struggling for hours trying to integrate passport with the existing bcrypt code in my project. I've read documentation, tried different things, and basically tortured myself for almost 15 hours. Can anyone take a look at my project and ...

Retrieve error message from 400 error in AngularJS and WebAPI

Why am I having trouble reading the error message in AngularJS from this code snippet? ModelState.AddModelError("field", "error"); return BadRequest(ModelState); Alternatively, return BadRequest("error message"); return Content(System.Net.HttpStatusCod ...

Transferring information from a component to a view using this approach

Having issues with 'this' I am struggling to retrieve data from the controller using 'this'. I attempted 'ng-init="meProfileCtrl.getView()"', but couldn't figure out how to assign the data properly like {{ user.name }} ...

Is it possible to reduce a field value in firestore after successfully submitting a form?

I have a variety of items retrieved from firestore: availability : true stocks: 100 item: item1 https://i.stack.imgur.com/hrfDu.png I am interested in reducing the stocks after submitting a form. I used the where() method to check if the selected item m ...

Here's a method for transferring data from one array to another: when there is existing data in the

Although this may seem simple to some, I have been struggling with it for hours without any success. If I have the following data in an array: var tDataValues = { id: "TenantID", text: "FullName", username: "Username", cnic: 'CNIC&ap ...

The file type input is not directing to the correct folder in Internet Explorer version 9

Could someone please assist me with this problem in IE9? After I select a file to upload, then choose another one, it shows "fakepath" in IE9. See the image below for more information: https://i.sstatic.net/QsJFk.png https://i.sstatic.net/3nWRC.png htt ...

In order to ensure compatibility, the Bootstrap UI Angular Accordian and Rails template should always be structured with

I've been attempting to implement the accordion directive in my Rails application, but I keep encountering an error that causes my browser to crash. Upon using the example accordion directive, I received the following error message: "Template for dir ...

Taking steps when a number is enclosed within a span

Testing a simple code with similar action to what I want. Apologies for any language errors, hoping to be understood :-) The HTML code snippet: <div class="pagination"> <a href="#" class=""><span>1</span></a> <a href=" ...

ngx-slick-carousel: a carousel that loops infinitely and adjusts responsively

I have implemented ngx-slick-carousel to showcase YouTube videos in my project. However, I am facing two main issues. Firstly, the carousel is not infinite, and when the last video is reached, there appears to be white spaces before it loops back to the fi ...

While iterating through the material-ui rating component, the 'index' value remains constant at 0 within the 'onChange' function

For my e-commerce React.js web app, I want to show the rating value of each product. Using the JS map function, I was able to track which product was hovered by its index ('onChangeActive' is the handler for this). {products.map((product, index) ...

Send the information to MongoDB in the form of an object, utilize it as a query, and then perform

I have a document stored in my mongoDB database. https://i.sstatic.net/2DI2p.png When a user types something into an input field on the frontend, for example 'test', the object passed looks like this: {'countdown': 'test'} ...

Creating a dynamic nested menu using React and JSON data

Creating a sidebar menu using data from a .json file, where the menu items are different BMW models. An example dataset is provided below: Here's my output: <List> {Menuhandler( menuDataHandler(JsonSeries) )} </List> The MenuDataHa ...

Utilizing AngularJS: Triggering a controller function from a directive

I am currently working on a project with a model named 'user', which includes a controller called 'login' and a directive called 'userMenu'. My goal is to have the userMenu directive utilize the 'login' controller th ...

Passing a JavaScript variable to PHP resulted in the output being displayed as "Array"

After sending a JavaScript variable with the innerHTML "Basic" to PHP via Ajax and then sending an email with that variable, I received "Array" instead of "Basic". This situation has left me puzzled. HTML: <label class="plan-name">Plan name: <b ...

how to implement dynamic water fill effects using SVG in an Angular application

Take a look at the code snippet here HTML TypeScript data = [ { name: 'server1', humidity: '50.9' }, { name: 'server2', humidity: '52.9', }, { name: 'server3', humidity: ...