Create an array of various tags using the ngRepeat directive to iterate through a loop

I'm familiar with both ngRepeat and forEach, but what I really need is a combination of the two. Let me explain:

In my $scope, I have a list of columns. I can display them using:

<th ng-repeat="col in columns">{{ col.label }}</th>

This will render:

<th>Col A</th>
<th>Col B</th>
<th>Col C</th>

Now, in my $scope, there's another variable called mode. When mode is set to 'admin', some admin-related HTML tags are displayed using ng-show. In this case, I would like my columns to be rendered as follows:

<th>config</th>
<th>Col A</th>
<th>config</th>
<th>Col B</th>
<th>config</th>
<th>Col C</th>

Is there a way to use ng-repeat to render both the 'config' and the label column simultaneously? Maybe something like:

<repeat for="col in columns">
  <th ng-show="mode == 'admin'">config</th>
  <th>{{ col.label }}</th>
</repeat>

Alternatively, do I have to create a new list that includes admin columns and regenerate it (using forEach) every time mode changes? What would be the best approach in this scenario?

Answer №1

If you want to iterate a list using AngularJS, consider utilizing the ng-repeat-start and ng-repeat-end directives. Here's an example:

  <div ng-repeat-start="item in items">{{ item.property }}</div>
    Content inside this section
   <div><div ng-repeat-end="">This part of the code will be repeated successfully</div>
  </div>

For more details, check out https://docs.angularjs.org/api/ng/directive/ngRepeat.

Answer №2

It is recommended to utilize a function that retrieves the columns dynamically

$scope.retrieveDynamicColumns = function() {
  if (mode != 'admin') {
    return columns;
  } else {
    var dynamicColumns = new Array(columns.length * 2);
    for (var index=0; index< dynamicColumns.length; index++) {
      dynamicColumns[index] = index % 2 == 0 ? 'config' : columns[parseInt(index/2)];
    }
    return dynamicColumns;
  }
}

template

<th ng-repeat="dynamicColumn in retrieveDynamicColumns()" ng-bind="dynamicColumn"></th>

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

Preventing exit in Ionic application for PhoneGap/Cordova development

I'm developing an application and I want to restrict access to users who are unfamiliar with it. Is there a way to accomplish this? The only solution I found so far is blocking the back button functionality... Appreciate your help! ...

Navigating the intricacies of platform-specific settings in JavaScript

Currently, I am in the process of transferring an application from one PHP-based CMS to another (such as Wordpress, Joomla, etc.). I have established classes that enable my code to function seamlessly on each platform without any alterations (so far so goo ...

Derive the property type based on the type of another property in TypeScript

interface customFeatureType<Properties=any, State=any> { defaultState: State; properties: Properties; analyzeState: (properties: Properties, state: State) => any; } const customFeatureComponent: customFeatureType = { defaultState: { lastN ...

Error: The Tabs component is expecting a different `value`. The Tab with the current `value` ("0") is not present in the document structure

I am encountering an issue while using MUI tabs. The error message I receive is as follows: MUI: The value assigned to the Tabs component is not valid. The Tab with this value ("0") does not exist in the document layout. Please ensure that the tab item is ...

Exploring Angular's ng-transclude directive within a repeat loop

Recently, I began delving into AngularJS and attempted to create a custom table directive with multiple slots for transclusion. However, I encountered an issue where the scope was not being passed to the transclude. Although there are various solutions ava ...

Steps for initiating a $.ajax POST request from a client to a server

Currently, I am working on a phonegap application where I need to transfer some data from an HTML file to a server and receive a response in return. Everything works fine when all the files are on the same server. However, once I separate the files between ...

Experiment with AngularJS and Jasmine by utilizing the stand-alone command line interface for testing

I have been working on a basic .js file and testing it with Jasmine. The tests run smoothly when using Jasmine's SpecRunner.html. However, I was wondering if it is possible to achieve the same results through a command line interface. I do have node ...

Getting data from an API with authorization in Next.js using axios - a step-by-step guide

click here to view the image user = Cookies.get("user") I'm having trouble accessing this pathway. I stored user data, including the token, using cookies. Please assist me with this issue. ...

How to efficiently manage drop-down menus on a website using VBA specifically designed for Internet Explorer

I am a beginner in VBA and coding, seeking assistance from the community. The purpose of this VBA code is to automate the following steps: Open Internet Explorer Enter user ID and password to login Select the current date Select a specific option (X1) fr ...

The Kendo UI Combobox triggering the change event twice

After noticing some strange behavior in all the comboboxes within my application, I realized that the Kendo UI ComboBoxes were triggering the change event twice, resulting in two HTTP requests being made when the code inside only required one. Despite cond ...

Having trouble retrieving AJAX response data using jQuery

I have been searching and attempting for hours without success. On my current page, I am displaying basic data from a database using PHP in an HTML table. However, I now want to incorporate AJAX functionality to refresh the data without reloading the page ...

Having trouble with Firebase continuously replacing old images with new ones whenever I upload them using JavaScript/jQuery

I am experiencing an issue with Firebase where it overrides my old pictures every time I upload a new picture. How can I configure it to save my old pictures as well? <!DOCTYPE html> <html> <head> <title>Firebase Storage< ...

What is the reason behind caching form data in an ajax call?

After the first successful submission, I am facing an issue where the original form data is being re-sent even when new values are submitted for a second attempt. How can I reset and reload the data to avoid this problem? I have tried several approaches i ...

The scroll event is triggered only when scrolling upwards

I am encountering an issue with my scroll function. Currently, it only alerts when scrolling to the top instead of the bottom. How can I correct this so that it triggers the alert when reaching the bottom of the page? $(window).scroll(function() { if ...

Exploring the Possibilities of Utilizing Multiple Endpoints with Apollo Angular GraphQL

mywebsite.com/graphql/categories mywebsite.com/graphql/account mywebsite.com/graphql/connection I'm working with multiple GraphQL endpoints on my server, each one having its own mutations and queries. I want to create an Angular frontend for it bu ...

Error occurred when trying to import an external module using an invalid hook call

I am creating a package named "Formcomponent" using React and React Bootstrap. This code is from index.tsx /** * Renders a component for a form. */ import React from "react"; import Form from "react-bootstrap/Form"; /** * List of props * @returns */ ...

Shifting the MUI DataGrid Pagination table to the left with CustomPagination: A step-by-step guide

Hey everyone, I am currently diving into the MUI data grid to gain a better understanding. In order to meet my design requirements for a table view, I have incorporated the DataGrid component from MUI. For pagination, I am utilizing their custom implementa ...

Troubleshooting issue: AngularJS not receiving NodeJS GET requests

I recently developed a web application for sharing photos. Currently, I am working on a route that is designed to fetch and display the photos of all users from an array. The code for the route is as follows: router.get('/getphotos',function(re ...

Keep rolling the dice until you hit the target number

Currently, I am in the process of learning JavaScript and one of my projects involves creating a webpage that features two dice images, an input box, and a button. The objective is for users to input a value, click the button, and then see how many rolls i ...

Using AJAX to dynamically update content via HTTP requests

Why do I keep seeing "loading..." instead of the content from data.php? xmlhttp = new XMLHttpRequest(); function fetchData () { xmlhttp.onreadystatechange = function () { if(xmlhttp.readyState = 4 && xmlhttp.status == 20 ...