Using AngularJS to create dynamic expressions with ng-repeat

I am working on creating a dynamic page that will display different lists based on certain aspects. The arrays I am using have different variable names, and I have been struggling to use a dynamic ng-repeat expression to achieve this. Can anyone offer some suggestions? I have created a fiddle to better explain what I am looking for.

<div ng-repeat="item in itemsToShow" >
    <!--I NEED THIS EXPRESSION TO BE DYNAMIC-->
 {{item.name}}, {{item.title}}

[fiddle] http://jsfiddle.net/eTTZj/867/

Answer №1

Your JSFiddle has been updated by me. To make it 'dynamic', simply implement the logic in the controller and assign different arrays to `$scope.itemsToShow`, avoiding any changes in your view.

Here is an example:

....
var show_items_2 = true;
/*Your custom logic here*/
if(show_items_2) {
   $scope.itemsToShow = items2;
} else {
    $scope.itemsToShow = items;
}

If you wish to have different `keys` within your array (such as 'name' and 'nameBig'), you need to access the items differently using `(key, value)`. Refer to this SO question.

<div ng-repeat="item in itemsToShow"></div>
<div ng-repeat="(key, value) in item">
    {{key}} : {{value}}
</div>

Answer №2

If you want to make use of operators, consider the following:

    <div ng-repeat="item in itemsToShow" >
     {{item.name || item.firstname}},  {{item.title || item.titleBig}}
   </div>

This method will select the existing property from your list.

For situations where managing many lists becomes challenging, you can try this approach:

<div ng-repeat="item in itemsToShow"></div>
    <div ng-repeat="(key, value) in item">
        {{key}} : {{value}}
    </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

Pressing the CTRL key and then the PLUS key simultaneously activates the zoom in

Is there a way to capture Ctrl + + / Ctrl + - events in Firefox using the following code: window.onkeydown = function(event) { if(event.ctrlKey && event.keyCode == 61) CtrlPlus(); if(event.ctrlKey && event.keyCode == 169) // or ...

Implementing multiple perspectives on a single webpage by keeping track of the browsing history with AngularJS and leveraging the

Within my AngularJS application, I have an about page that contains various sub-sections, all within the same template. My goal is to have each section accessible through its own unique URL that will automatically scroll down to the corresponding section i ...

Incorporating dynamic elements without sacrificing the original static page

I have a compilation of video titles. Each title links to a page featuring the specific video along with additional details such as player information, description, and discussion. <div id="video-list"> <a href="/Video/title-1">Title 1</a&g ...

What sets apart Selenium's mouseMove() function from the physical movement of a mouse?

Imagine I have element A and element B on a webpage. Using tools like Selenium or PhantomJS, I can manipulate the mouse via coordinates. By identifying the position of element A (a link) and element B (a submit button), I can create a bezier curve or mimi ...

Cancel the starting path in Angular 9 and initiate a redirection

I am attempting to change the initial path such as localhost:4200/ so that it displays an error view instead of the home page. I want the home page to only be accessible if you navigate to something like localhost:4200/tag1/tag2. I need to be able to capt ...

Unable to display icon using the fontawesome react-js library

import "./App.css"; import { FontAwesomeIcon } from '@fortawesome/react-fontawesome' function App() { return ( <div className="container"> <input type="text" className="form-control" placeholder ...

Showcase a trio of randomly selected images simultaneously using Angular

I'm curious to learn how I can use Angular to display three random images when a button is clicked. Can anyone guide me on this process? Appreciate the help! ...

Leveraging data schemas to manage the feedback from APIs

I am curious about the benefits of modeling the API response on the client side. Specifically: First scenario: const [formData, setFormData] = useState(null); ... useEffect(() => { const callback = async () => { try { const fetchDa ...

Encountered an issue retrieving tweets from the Twitter API 1.1

I recently completed an online tutorial from this site: However, I'm encountering an error message that simply says 'error: ' without any additional information. To start, here is my PHP script used to fetch the JSON output: <?php sess ...

Having trouble accessing the datepicker after hitting the escape key

When using an AngularJS date picker on a read-only textbox, the date picker pops up upon clicking on the textbox. However, pressing "esc" will close the date picker, but clicking on the text box again does not reopen it. Instead, we have to click outside ...

What could be the reason for this JSON being considered "invalid"?

Despite passing validation on jsonlint, both Firefox and Chrome are rejecting this JSON: { "messages": [ { "subject": "One" }, { "subject": "Two" }, { "subject": "Three" ...

What is the best way to prevent selection based on the ng-model in AngularJS?

Can you help me disable the Proxy input field for selection until an Attestor is selected? Currently, both fields are read-only, but if the attestor is not selected, the user should not be able to select the Proxy. I am new to angularJS and would appreciat ...

The gadget is displaying as "undefined"

I am working on a project that involves mapping devices based on the videoinput. I am trying to display the values, but it seems that the device is showing as not defined. Can anyone help me figure out what mistake I might be making here? Thank you in ad ...

The Node.js script functions correctly on the first run but encounters failure in subsequent executions

I am in need of a Node.js script that can perform the following tasks: 1 - Trigger when an image is added to a specific S3 bucket. 2 - Generate a thumbnail of that image (360x203 pixels). 3 - Store a copy of the thumbnail in a separate S3 directory. ...

AngularJS encounters bad configuration on 'GET' request

I am facing an issue with my API that returns data to AngularJS based on a given ID. When the data is returned as JSON, AngularJS throws a 'badcfg' error, indicating that it could be due to the format of the returned data. I'm struggling to ...

Does running npm install automatically compile the library code as well?

I have a query regarding npm and its functionality. I also posted the same question on Reddit, but haven't received a satisfying answer yet. Let's use the jQuery npm package as a case study. Upon running the command npm install jquery, I notic ...

Dynamic CSS Changes in AngularJS Animations

I am currently working on a multi-stage web form using AngularJS. You can see an example of this form in action by visiting the link below: http://codepen.io/kwakwak/full/kvEig When clicking the "Next" button, the form slides to the right smoothly. Howev ...

Remove the </div> text and replace it with nothing

I'm attempting to substitute the end tag div with an empty string. Here is my code: $('#textDiv').html().replace(/'</div>'/g,'').replace(/<div>/g,'\n') This is the HTML: <div id='tex ...

What is the best way to split a semicircular border radius in two equal parts?

Is there a way to halve the yellow line or remove it from above the red box, while keeping it below? Can this be achieved using just HTML and CSS, or is JavaScript necessary? * { margin: 0; padding: 0; box-sizing: border-box; } body { height: 1 ...

The code that functions properly in Internet Explorer and Chrome is not functioning as expected in Firefox

I created a jQuery function that is designed to allow only numbers and one decimal point in an input field of type text. While it works perfectly fine in Internet Explorer and Chrome, I've encountered an issue with Firefox where it doesn't seem ...