Class name that changes dynamically with a specific prefix

I am attempting to dynamically assign ng-model names so that they can be accessed in the controller using '$scope.numbers.no1', '$scope.numbers.no2', and so on. However, my current code is not producing any results:

<div ng-repeat="row in numbersArray" class="numbersRow">
    <div ng-repeat="num in row" class="number" ng-model="numbers['no' + num]">{{num}}</div>         
</div>

In my controller:

$scope.numbers = {};
$scope.numbersArray = [[1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
                       [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
                       [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
                       [31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
                       [41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
                       [51, 52, 53, 54, 55, 56, 57, 58, 59, 60],
                       [61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
                       [71, 72, 73, 74, 75, 76, 77, 78, 79, 80]];

To test, I am running:

$scope.numbers.no1 = 'A';

However, nothing is happening. Can someone identify what mistake I am making?

UPDATE: Upon further investigation, I realized that I mistakenly used an ng-model on a div. What I actually want to accomplish is to randomly select a number from the 80 available numbers and change its background color to black to indicate that it has been selected.

Answer №1

https://example.com/code-sample

<div ng-repeat="item in itemsArray" class="itemsRow">
    <div ng-repeat="element in item" ng-class="{'highlightedElement': items[element]}"
         ng-click="items[element] = !items[element]" class="element">{{element}}</div>         
</div>

Answer №2

The provided code iterates through the array $scope.numbersArray and assigns it to $scope.numbers using the input value as the key. Here's the code snippet you can use:

<div ng-repeat="row in numbersArray">
  <div ng-repeat="num in row">
    <input ng-model="numbers['no' + num]" /> {{num}}
  </div>
</div>

Check out the Demo on JsFiddle

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

What is the process for transitioning from imports with <script> tags to a module bundler system?

Currently, my project utilizes Angular without a task manager or dependency manager. All libraries are stored in the repository and included using <script> tags such as <script src="libs/angular/angular.min.js"></script>. In an effort to ...

Is it possible to conceal JavaScript comments on a page before it is displayed?

Curiosity has led me to ponder a question that may seem trivial. Consider this scenario: in my HTML or ASPX page, I include a comment for some JavaScript code. When the page loads, will the comments be rendered along with the rest of the page's conten ...

Performing numerous asynchronous MongoDB queries in Node.js

Is there a better way to write multiple queries in succession? For example: Space.findOne({ _id: id }, function(err, space) { User.findOne({ user_id: userid }, function(err, user) { res.json({ space: space, user: user}); }); }); It can g ...

Using the $inc operator in mongoose to avoid decrementing a value below zero

My code successfully deducts credit from a user using $inc in Mongoose, but the concern is that the value can become negative, which is not ideal. Is there any way to prevent this? module.exports.deduct_credit = function(subscriber_email,callback){ Us ...

What are the most effective techniques for utilizing JavaScript modules in both the Server and Browser environments?

Currently, I am in the process of developing a JavaScript project that utilizes NodeJS. There are certain objects that need to be shared between the client and server side. I attempted to employ the module system in Node, but struggled to find an appropria ...

Leverage the power of React in tandem with Express

My web site is being created using the Express framework on NodeJS (hosted on Heroku) and I'm utilizing the React framework to build my components. In my project, I have multiple HTML files containing div elements along with React components that can ...

Guide on detecting errors when parameters are not provided with req.params in Node.js

My question revolves around a piece of code that I have been working on. Here is the snippet: const express = require('express') const app = express() app.get('/test/:name', (req, res) => { const {name} = req.params; res.send(`P ...

I am facing an issue with Angular reactive forms where the default values I set in ngOnInIt are not being reflected

Having some issues with setting default values in my Angular app using reactive forms. The defaults I set in ngOnInit are not showing up. I am also using the filter function within the map method. I am trying to select a value based on the URL and have it ...

Tips for showcasing a Bootstrap alert

I'm attempting to integrate Bootstrap Alerts into my project, but I'm struggling to display them programmatically. Here is the HTML snippet: <div class="alert alert-success alert-dismissible fade show" role="alert" id="accepted-meal-al ...

jquery interval randomly selecting a new option instead of verifying the correct answer

Below is a jQuery game created with aspx that runs on a 3000 millisecond interval. $(document).ready(function () { var interval; //initialize timer interval = setInterval(function () { //create timer ...

Tips for displaying bar chart labels effectively with ChartJS

I am working on an Angular project and I would like to incorporate a barchart using ChartJS. The data for this chart can vary in size, sometimes being very large. One issue I have encountered is that when the number of data points is large, the labels ove ...

Encountering a message error for the Collapse component in Material UI

I'm currently working on creating a hamburger menu using Material UI List in Next.js, inspired by the Nested List example in the Material UI documentation. However, I've encountered an error when using "collapse" in my code. The rest of the code ...

Supply a JSON parameter as a variable into the .load() function

When a button is clicked, I am attempting to load a page using the .load() method with a POST request. The URL parameters are generated and displayed as JSON formatted in the button attribute btn-url. Problem: The parameter is not being passed to the .loa ...

Is there a way to convert time measurements like minutes, hours, or days into seconds in React and then pass that information to an

Currently, I am working on an application that allows users to select a frequency unit (like seconds, minutes, hours, or days) and input the corresponding value. The challenge arises when I need to convert this value into seconds before sending it to the ...

What are the common reasons for HTTP service errors in AngularJS?

I've included some code below: app.js app.controller("carCtrl", function($scope, $http, $window) { $scope.createTenCar = function() { $http({ method: "POST", url: "http://localhost:8080/EX11_29112018_spring_restful/cars" }).then ...

The React implementation of an OpenLayers map is not responsive on mobile devices

I recently set up an OpenLayers map in a React project and it's working smoothly on desktop browsers. The map is interactive, allowing users to drag, zoom in/out, and display markers as expected. However, I'm facing an issue with touch events on ...

Remove an item using a function embedded within it

I've been grappling with this issue for quite some time now and I'm unable to find a solution. My approach involves Vue(JS). What I'm attempting to achieve is to push notifications into an Object and then present them to the user. Each noti ...

Receiving a JSON object in response after sending data to a server using JavaScript

I am facing an issue with passing an email and password on login click to a database using PHP. After testing the email and password combination, PHP sends back a JSON object. While I have verified that the PHP code is functioning correctly and returns a J ...

The values of React children props will always remain consistent

While attempting to incorporate an ErrorBoundary HoC component for error handling following the guidelines from React16 documentation, I designed the ErrorBoundary component as a PureComponent. It became apparent that the children props remained consistent ...

Reduce the size of a container element without using jquery

In my Angular application, I have structured the header as follows: -- Header -- -- Sub header -- -- Search Box -- -- Create and Search Button -- -- Scroll Div -- HTML: <h1> Header </h1> <h3> Sub header </h3> <div class="s ...