Utilizing Angular's dynamic property names within a nested ng-repeat loop

Is there a way to dynamically add generated names in nested ng-repeat loops? For example:

    <div ng-repeat="x in JSONfile">
      <div ng-repeat="i in x.name">
         <span><a ng-href="{{i.link}}">{{i.name}}</a></span>
      </div>
    </div>

The JSONfile returns some names,

x.name is dynamically generated from the mentioned JSONfile, and it should be used as plain text like "NAME". If I add 'NAME' instead of i.name, the JSON file is loaded, but I want it to automatically load because I don't know which name will come first.

i.name currently returns each character separately (n, a, m, e) instead of "NAME" as expected...

So my question is, can Angular be told to treat this dynamically generated value as I typed it?

If I use ng-repeat="i in Tom", it will return the json data, but with x.name it doesn't work.

Thanks!

EDIT (added json):

var brojVijesti = [];
$scope.JSONfile = brojVijesti;

            // LNG Json
            $http.get("/LEADERBOARDv2/jsons/LNG.php").then(function(response) {
                var LNG = response.data.LNG;
                $scope.LNG = LNG;
                $scope.LNGbroj = LNG.length;
                brojVijesti.push({"name":"LNG", "number":LNG.length});
            });

            // DT JSON
            $http.get("/LEADERBOARDv2/jsons/DT.php").then(function (response) {
                var DT = response.data.DT;
                $scope.DT = DT;
                $scope.DTbroj = DT.length;
                brojVijesti.push({"name": "DT", "number": DT.length});
            });

Answer №1

I suggest using i in x instead of i in x.name

Update: You can utilize the $parse dependency to access the variable with that specific name from $scope.

<div ng-repeat="x in JSONfile">
  <div ng-repeat="i in x">
    <span><a ng-href="{{i.link}}">{{getVariable(i.name)}}</a></span>
  </div>
</div>

JS:

$scope.getVariable = function(variableName) {
   //evaluate the variable name within scope's context.
   return $parse(variableName)($scope); 
}

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

Conversion of POST Body to Object in MVC is mysteriously hiding a thrown exception

Currently, I am implementing some basic validation in my model classes. Within my current project, there exists a model class as shown below: public class SimpleRequest { string _message; string _otherProperty; public Message { get { ...

Unable to establish an external connection with the node

Currently, I am in the process of establishing a connection to a local server and running an app on port 8080 using node. Both node and apache are installed on my system. When Apache is active, I can access the server externally. However, when Node is runn ...

Styling HTML elements with CSS to create a full width underline effect

Is there a way to make the underlines/borders full width for each line in a paragraph without adding line breaks? I'm seeking suggestions on how to achieve this. Two potential solutions I've considered are using the tag or creating an image, ...

Executing npm commands programmatically from a node.js script

Currently, I am developing a specialized command line interface (CLI) for managing various packages to be installed or uninstalled using npm. Should I execute npm through spawn('npm') or require('npm')? require('child_process&apos ...

Performing operations on an array: What method do you favor and why? Is there a more efficient approach?

What is the most effective method for checking if an element exists in an array? Are there alternative ways to perform a boolean check? type ObjType = { name: string } let privileges: ObjType[] = [{ name: "ROLE_USER" }, { name: "ROLE_ ...

Angular Material selectionChanged function is providing the previous step instead of the current step

I need to make a page with two columns: one for a vertical stepper and the other for step descriptions. I want the description to update based on the current step selected. However, I am running into an issue where the selectedIndex shows the previously ch ...

How can I remove unnecessary components from an API result in discord.js before sending it?

The particular API being discussed is a pun-themed one, specifically this one. Here is the code snippet I am currently utilizing: const superagent = require("superagent") module.exports = { run: async(client, message, args) => { const pun = a ...

Deleting a row from a b-table made easy

Hello, I have a question about deleting an item or row in a b-table. I attempted to delete the item by using @click.prevent="deleteCustomItem(data.data.item), but I encountered issues because I am in strict mode. Here is my table setup: <custom-item- ...

React Select is failing to display the selected value

I'm currently learning react-redux and encountering a couple of issues. In my code, I am using React Select Plus in the following manner: <Select id="portf" options={opts} onChange={value => portfolioSelector(value ...

Oops! Looks like the file or directory at '/home/arpit/.steampath' couldn't be found

Whenever I attempt to execute npm start, I encounter the following error: [Error: ENOENT: no such file or directory, stat '/home/arpit/.steampath']. Despite trying various solutions like removing and reinstalling node modules and NPM, as well as ...

Trigger file download with ajax and php

I have been using an image picker plugin to select an image and then force it to download. Initially, I hard coded the PHP which worked perfectly fine. The download pop-up appeared and I was able to view the file without any issues. However, when trying to ...

Sending specific names as properties

Looking to streamline my repetitive form inputs by abstracting them into a component, I want the following functionality: <InputElement title="someTitle" v-model="someName" @blur="someFunction" name="someName" type="someType"> The desired output wo ...

Refresh the page excluding any <script> elements

I am looking to display a page without actually running its JavaScript code, while also injecting my own script and providing the user with a perspective similar to that of a bot. One approach I have considered is loading the page via ajax, stripping out ...

unable to retrieve an object's property

Currently, I am implementing a JWT token compare function to authenticate user login by comparing the user password. However, I am facing an issue where I cannot access the user password after executing the mongoose query. exports.login = async(req, res, n ...

Tips for creating a customized dropdown menu that opens upwards without relying on Bootstrap

Looking for some assistance with creating an animated dropdown menu that slides upwards. Any help is appreciated! $('.need-help').click(function() { $('.need_help_dropdown').slideToggle(); }); .need-help { background:url(../im ...

The Datetimepicker function outputs a blank string when selected

I'm currently using Bootstrap datetimepicker for date ranges in my project. I've been attempting to retrieve the date value in an AngularJS controller as shown below. Despite trying various methods from Stack Overflow posts, I consistently receiv ...

Using JQuery to Iterate Through All Form Inputs

I am attempting to retrieve the values of all input fields from a form using JQuery and store them in an array to be sent via AJAX with a GET request. My initial approach did not yield the desired results: function gatherFormData(){ $('#formId i ...

Error in Webpack: JSX elements that are adjacent must be enclosed within a wrapper tag

After adding a new component and integrating it into my Main component, I encountered an error when running webpack. The error message displayed was: "Adjacent JSX elements must be wrapped in an enclosing tag" Below is the snippet of code where the iss ...

Is there a way for me to transform a CSV file into JSON just the way I

Hello, I am encountering a problem with converting my CSV to JSON. The result is not exactly as expected. In my main.py file: import csv filename = "forcebrute.csv" # opening the file using "with" statement with open(filename, 'r') as data: ...

I would like to exclude the item within my ng-repeat loop using ng-if

I'm attempting to utilize ng-if within ng-repeat in order to create Accordions. Depending on the condition value, I want certain items to be skipped in the ng-repeat. For example, if item.condition is true, then only display the accordion. The code b ...