Leveraging the power of Angular's ng-repeat for iterating through multi-dimensional arrays

Currently, I am in the process of developing a game and utilizing NG repeat to dynamically insert divs based on the number of letters and words present. For instance, if the answer to a question in the game is "Clean Sheet," I would like NG-Repeat to generate the appropriate columns so that it displays __ __ __ __ __ space __ __ __ __ __ for the user to input their response. The code snippet I have written so far looks like this:

try{
//assuming $stateParams.answer is "clean sheet"

var answerArr = $stateParams.answer.toString().split(' ');
var finalLines = "";
$scope.mainWordHolder = [];

angular.forEach(answerArr, function(value, key) {
     var amt = value.length;

     $scope.amtofLetters=[];
      for (var i=0; i<amt; i++) {
          $scope.amtofLetters.push(i);
      }
      $scope.mainWordHolder.push($scope.amtofLetters);
      $scope.amtofLetters = [];
});

console.log($scope.mainWordHolder);
}catch(e){console.log("error : "+ e);}

At this stage, the $scope.mainWordHolder contains:

[ [ 0, 1, 2, 3, 4 ], [ 0, 1, 2, 3, 4 ] ]

This output aligns with my desired outcome as it outlines the necessary spaces for each word. How can I implement ng-repeat to display these as divs, allowing me to create a keyboard interface where the user can input keys similar to other popular games?

I attempted the following:

<div class="row">
<div ng-repeat="content in answerArr" class="col"> 
       <div ng-repeat="contentt in mainWordHolder" class="col">

      </div>   
</div>
</div>

However, I encountered issues as nothing appeared on the screen. Any assistance on this matter would be greatly appreciated.

Answer №1

It seems like you want to reference answers within your ng-repeat, but the variable answers is not defined in the scope.

Make sure that when you use ng-repeat="item in answers", the variable answers is properly stored on the scope for it to be accessible in the HTML.

To access the contents of answers through ng-repeat, ensure that you have stored it on the scope first.

Answer №2

In order to properly iterate over the 'content' representing the array within the entire outer array, you can use the following code:

<div class="row">
<div ng-repeat="content in answerArr" class="col"> 
       <div ng-repeat="contentt in content" class="col">

      </div>   
</div>
</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

Utilizing Array Formulas in VBA for Excel

I encountered an issue while populating data in my VBA-created array. Although I included formulas in the macro, they are not functioning properly when pasted. Sub Button3_Click() Application.Calculation = xlManual 'initializing variables Dim i A ...

Would it be unwise to create a link to a database directly from the client?

If I want to connect my React app to Snowflake all client-side, are there any potential issues? This web app is not public-facing and can only be accessed by being part of our VPN network. I came across this Stack Overflow discussion about making API cal ...

Set the value of a key in an array to the output of a function

I have incorporated a plugin on my wordpress site that enables me to generate team member profiles. I am currently inserting code into the author.php file within my child theme in order to showcase the 'Our Team' plugin content on the author arch ...

What is the best way to prevent the onClick event from triggering during the page rendering process?

I am currently working with React, Gatsby, and Material UI Buttons. I'm facing an issue where the most recently pressed button is getting disabled along with all other buttons when running my code. Despite already implementing bindings, as suggested b ...

Setting the open date in Bootstrap-DatePicker without configuring a default selection

Is it possible to modify the open-to date of the bootstrap-datepicker in AngularJS without populating a field with a default date value? Instead, I simply want to adjust the date that the datepicker displays initially. Any insights on how this can be ach ...

Navigating through the elements of the DOM

Here is a function that deletes people from a list when you click on a certain part of the form: function ParticipantsDeleteClick(model, url) { for (i in model.Participants) { $("#delete" + i).click(function () { $.ajax({ url: url, ...

Issues with the .change(function() JavaScript in Internet Explorer versions less than 9

I'm experiencing issues with this script in Internet Explorer versions below 9. Can someone please help me identify what is wrong with my script? Thank you. IE7 and IE8 are showing the following error: SCRIPT87: Invalid argument. Found ...

What is the best way to access the phone's dialer?

I'm attempting to open the phone dialer in my worklight 6.2 hybrid application by clicking on a button and/or an anchor tag. See the code I've been using below: Button: <button onClick='window.parent.location.href = "tel:+1xxxx"'&g ...

Mistaken data retrieved from a knockout observable array

My current project involves developing a web application using Asp.Net Mvc, where I am utilizing knockout Js to retrieve and send data to the Html View after manipulating the data. For instance, let's consider the data in an array called datainput: ...

Update the color of the div when all checkboxes in the hidden form are selected

Seeking help for multiple issues I'm facing with my code. Here is the link to the code. HTML for Upper Table: <div class="block"> <table> <tr> <th>Nr.</th> <th style="width: 200px">Task</th& ...

The Vue store array declaration triggers a TS error stating that it is not assignable to a parameter of type never

I'm puzzled as to why this error keeps showing up: Argument of type '{ id: string; }' is not assignable to parameter of type 'never'. ... appearing at const index = state.sections.findIndex((section) => section.id === id); T ...

What is the reason for meshes rotating around their axis while Objects3D rotate around the world axis?

Consider the following code snippet: var geometry = new THREE.BoxGeometry( larguraX,altura,comprimentoZ); var material = new THREE.MeshBasicMaterial( {color: "pink"} ); var mmesh = new THREE.Mesh( geometry, material ); var objj = new THREE.Object3D( ...

Reusing methods in Javascript to create child instances without causing circular dependencies

abstract class Fruit { private children: Fruit[] = []; addChild(child: Fruit) { this.children.push(child); } } // Separate files for each subclass // apple.ts class Apple extends Fruit { } // banana.ts class Banana extends Fruit { } ...

Eliminate a specific element from the dataset

My question revolves around a data array used to populate three drop down <select> boxes. How can I eliminate duplicate values within the drop downs without affecting the array itself? For example: data = { firstbox_a: ['grpA_1','gr ...

What is the best way to nest a v-for inside another v-for in Vue.js?

BaseAccordion.vue <template> <div class="wrapper"> <div class="accordion"> <input type="checkbox" @click="toggleItem" /> <h2 class="title"> <slot name="title"></slot> </h2> ...

Choose a drop-down menu with a div element to be clicked on using Puppeteer

Issue Description: Currently encountering a problem with a dropdown created using material select. The dropdown is populated through an API, and when selected, ul > li's are also populated in the DOM. Approaches Tried: An attempt was made to res ...

Using Middleware to Access LocaleStorage in Universal Mode

Here's the dilemma: My understanding is that accessing LocaleStorage from Middleware in Universal mode is currently not possible (at least not out-of-the-box.) Question: Is there any way to solve this issue? Are there any modules or solutio ...

Issue with facebook button in reactJS where onClick() event is not being triggered

I'm facing an issue where my onClick handler is not working with the Facebook button from https://developers.facebook.com/docs/facebook-login/web/login-button/. However, it works fine with a regular div element. Any thoughts on why the onClick event ...

Loop through each JSON object and insert it into an HTML element

I am working on looping through a JSON object and converting it into HTML. While I can iterate through all the objects in the JSON, I am having trouble extracting just the first object. var res = '[{"ID":"246","mobile":"samsung","feedback":"feedback ...

Creating unique custom renders in Node.js

I am in the process of creating an API using node.js and express. I am interested in enhancing the standard res.send function from any of the external route files to format the response beforehand and include extra data. Is there a way to achieve this? A ...