Using Angular's ng-repeat directive in combination with an input field

Here's a view snippet I'm working with:

<table class="table">
   <tr data-ng-repeat="exercise in exercises x">
      <td>
           <input type="number" data-ng-model="?????????" />
       </td>
        <td>
           {{exercise.Name}}
        </td>
     </tr>
  </table>

I'm trying to figure out what value should go into the data-ng-model attribute for two-way data binding to work correctly so I can access the input's value in my controller.

I attempted using

data-ng-model="{{exercise.Name}}"
, but it caused errors.

Additionally, how can I reference specific inputs in the controller? Is there a way to do something like: $scope.InputOne = ...

Answer №1

Utilize data-ng-model="exercise.Name" without including the {{}} brackets.

A helpful starting point can be found at the angular tutorial page.

Answer №2

Utilize ng-model for two-way binding, and ng-bind or {{}} brackets for one-way binding.

This illustrative example showcases the use of both methods and how to access object information.

Note: The controller should not have direct visibility into the view.

<body data-ng-app="app">
 <div data-ng-controller="TestCtrl">
  <table>
    <tbody>
      <tr data-ng-repeat="exercise in exercises">
        <td>
           <input type="number" data-ng-model="exercise.Name" />
        </td>
        <td data-ng-bind="exercise.Name"></td>
        <td><button data-ng-click="getInformation($index)">Get information</button></td>
      </tr>
    </tbody>
  </table>
</div>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.4/angular.min.js"></script>

var app = angular.module('app', []);

app.controller('TestCtrl', function($scope) {

$scope.exercises = [{Name:1},
                    {Name:2},
                    {Name:3},
                    {Name:4}
];

$scope.getInformation = function(index) {
  alert($scope.exercises[index].Name);
}        
});

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

Issue with localstorage in app startup

Below is the code snippet I am working with angular .module('core') .factory('jsonstorage',function($rootScope,$http,$localStorage){ return { set: function(entity, value) { $rootScope.$storage = $localStorage; ...

What is the best way to utilize a for-loop for updating values in a JavaScript variable?

Recently, I developed a code snippet that is capable of generating random 12-digit codes using Math.random and for-loops. Users can input the desired number of codes they want to generate on the web page. However, I am facing an issue with storing these r ...

Exploring the JSON object and dynamically incorporating elements into it is my goal as I navigate through AngularJS

I'm seeking a way to display the entire JSON data in a tabular format, with the ability to dynamically loop through any additional values that are added. Here is an example of my current JSON: $scope.tableContent = [ { id: 1, ...

Top method for referencing HTML layout across various pages

Is there an optimal method for consistently calling an HTML structure from various pages? In my webapp, I have the same type of structure that needs to be called from different pages. For instance, I have a media management page displaying uploaded media ...

Is It Possible to Run Javascript Code Based on a Specific Screen Size?

Here is the Javascript code I'm currently using to control the mobile menu functionality on my website. It prevents content outside of the menu from scrolling when the mobile menu is open and closes the menu when an item inside it is clicked. document ...

Revamp your code by utilizing ES6 class to replace multiple if statements in Javascript

Currently, my code is filled with numerous if statements and I am considering a switch to classes in ES6. However, Javascript isn't my strong suit... so PLEASE HELP ME..! Previous code: //example code function first(){ console.log('first sce ...

Build a Node.js application with Express to host static files

I am attempting to provide my static files "web.html" and "mobile.html", but I want them to be served only if the user is accessing from a web or mobile device. After some research, I came up with this code: var express = require('express'); va ...

Prevent user input in calendar view using HTML classes

Need help adding a date picker to my code. Below is the snippet of code: The issue I am facing is that I want to prevent users from manually typing in dates. <div class="form-group clearfix"> <label class="col-lg-4 control-label">Date Sold ...

When using AngularJS and Require together, there may be instances where the r.js

There seems to be a ongoing discussion about whether or not to use require.js with AngularJS, however, I have decided to implement it in my project. Currently, the project is set up and running with require, and my next step is to optimize and minify using ...

What is the best way to send a JSON Object containing option parameters in order to open a new window?

Have you noticed the interesting parameter list for the window.open() object? Is there a possibility to use window.open({options..}); instead? What are your thoughts on this matter? ...

Updating the icon image for navigation in AngularJS

I am looking to update an image upon navigating to /tab1. This can be achieved using 'ng-click' with AngularJS routing. Sample HTML snippet: <div class = "body" ng-controller = "app"> <div class = "column1"> <div cla ...

Identifying errors in a React component upon loading

As I delve into my project, my main focus lies on detecting errors within a component. The ultimate goal is to seamlessly redirect to the home page upon error detection or display an alternate page for redirection. I am seeking a programmatic solution to ...

Utilizing Angular JS to ensure services are universally accessible across controllers and views

Imagine we have a service like this: myApp.factory('FooService', function () { ... Now, from a controller, the code would look something like this: myApp.controller('FooCtrl', ['$scope', 'FooService', function ($s ...

Show that a CPU-intensive JavaScript function is executing (animated GIF spinners do not spin)

Displaying animated indicator or spinner gifs, then hiding them, is an effective way to communicate to the user that their action is being processed. This helps to assure the user that something is happening while they wait for the action to complete, espe ...

Prevent button interaction until completion of modal animation using Bootstrap 4

I have incorporated a Bootstrap 4 modal in my website to display a large navigation menu. The navigation is triggered by a toggle button with a CSS animation that transforms a hamburger icon into an X. However, I encountered an issue where if the button i ...

I recently created a "dist" folder through Babel. Is it possible to integrate the files within this folder directly into a fresh React project?

I have a React library that is available on npm. My process for publishing it involves the following steps: To begin, I create a folder called dist using Babel by executing this command- babel ./src -d ./dist Next, I upload the resulting dist directory ...

Display the contents of a post within the same page using ReactJS

I am currently in the process of setting up a blog. In my index.js file, I have a left sidebar that displays the links to the articles. My goal is to have the post content show up in the right sidebar of the index.js file when I click on a link, instead of ...

The alignment issue of Owl Carousel card being off-center

I can't seem to get my owl carousel properly centered on my website. I've set up the owl carousel to showcase customer testimonials on my landing page, but it's slightly off to the right and not aligning correctly with the testimonial title. ...

The second JSP page fails to load following an AJAX POST request

The following code snippet is found in page1.jsp. $.ajax({ type: "post", url: "page2.jsp", data: newdata, success:function(msg){ return msg; } ...

The error message "Error: [$http:badreq] - Angular JS" is being displayed when attempting to use $inject in a service

Upon submitting the registration form, I encountered an error while trying to save the data using an Angular service. The specific error message received was Error: [$http:badreq] Below is the content of my register.controller.js file: (function(){ v ...