Using the key from the parent ng-repeat to filter ng-repeat

Here is an example:

<div class="row" ng-repeat="(key, value) in sections">
    <h5>{{ value }}</h5>
    <ul>
        <li ng-repeat="item in $parent.items | filter: key">{{ item.name }} -- {{ item.section }}</li>
    </ul>
</div>

The goal is to filter the nested ng-repeat of items based on the section key.

This setup will allow each item to display related items, where {{ item.section }} matches {{ value }}

Answer №1

To meet the requirement of iterating over an object and filtering the child ng-repeat by using the key of its parent, you can implement a custom function that returns the desired object. Below is a sample application illustrating this concept.

var app = angular.module('sample', []);

app.controller('samplecontroller', function($scope) {

  $scope.filterKey = function(value) {
    return {
      name: value
    };
  };

  $scope.categories = {
    'John': 'English'
  };
  $scope.posts = [{
    name: 'John',
    category: 'General'
  }, {
    name: 'James',
    category: 'Restricted'
  }]
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>

<body ng-app="sample">
  <div ng-controller="samplecontroller">
    <div class="row" ng-repeat="(key, value) in categories">
      <h5>{{ value }}</h5>
      <ul>
        <li ng-repeat="post in $parent.posts | filter: filterKey(key)">{{ post.name }} -- {{ post.category }}</li>
      </ul>
    </div>
  </div>

</body>

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

Prevent a sliding panel from responding if there is no input text by incorporating jQuery

I have a straightforward example of user input and a button that reveals "Hello World" when clicked: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1 ...

Using Typescript with Angular 2 to Implement Google Sign-In on Websites

Currently, I am in the process of creating a website that utilizes a typical RESTful web service to manage persistence and intricate business logic. To consume this service, I am using Angular 2 with components coded in TypeScript. Instead of developing m ...

Steps for adding a row as the penultimate row in a table

There aren't many solutions out there that don't rely on jQuery, but I'm looking to avoid it. The row content needs to be generated dynamically. Here is my flawed approach: function addRow() { let newRow = '<tr><td>B ...

Creating a dynamic and interactive table with Angular and the amazing angular-responsive-tables library

I am currently working on creating a responsive table using AngularJS. To demonstrate what I am trying to achieve, I have put together an example in this fiddle: https://jsfiddle.net/loredano/xnyzaLnu/1/ <tr ng-repeat="product in products" > &l ...

AngularJS is patiently awaiting the completion of the translation rendering process

I implemented the ngCloak directive to my body element on the page and every angular-related element, but it appears that it is not working with AngularJS translate. The translate variables show up on the page initially and then get translated after a se ...

Issues with JQuery Ajax file upload functionality in the client's browser causing upload problems

Seeking to integrate a profile picture upload feature with Ajax and JQuery I've been successful in uploading profile pictures to my database on various machines and mobile devices, including Chrome, Edge, Firefox, Safari, and Vivaldi. However, my cl ...

Encountered a problem during the installation of tensorflowjs for node | Received an error: Command failed: node-pre-gyp install

While attempting to install @tensorflow/tfjs-node, I encountered the following error: Command failed: node-pre-gyp install --fallback-to-build. Is there a solution to resolve this issue? Below is the complete log: npm ERR! code 1 npm ERR! path E:\ ...

To prevent the background window from being active while the pop-up is open

I have a link on my webpage that triggers a pop-up window, causing the background to turn grey. However, I am still able to click on other links in the background while the pop-up is open. I tried using the code document.getElementById('pagewrapper&ap ...

An error message saying "An unexpected { token was found at Function (<anonymous>)" occurred while running an iteration in a .pug file

How can I display an array of strings in a pop-up screen when a specific option is clicked? I have set up a Modal service and passed the necessary elements. Modal Service: module.exports = function PopupModalServiceFactory($uibModal) { var service = ...

Tips for simulating a service in Angular unit tests?

My current service subscription is making a promise: getTaskData = async() { line 1 let response = this.getTaskSV.getTaskData().toPromise(); line 2 this.loading = false; } I attempted the following approach: it('should load getTaskData', ...

What is the best way to have a flexbox item occupy the available space and enable scrolling?

Trying to create a vertical scrollable div within the remaining space of various elements that do not have fixed heights. I currently have a flexbox element that adjusts to its content, but what I actually need is for the element to fill up the empty spac ...

Dynamic counter with Javascript or Ajax functionality

Is there a way to create a counter using JavaScript or Ajax that preserves its count even when the page is refreshed? <script type="text/javascript"> var totalCount = parseInt(localStorage.getItem('counter')) || 0; document.getElementById ...

When you're downloading a file in Safari, the filename ends up being displayed as 'unidentified'

I am encountering an issue with the code I implemented that downloads a file in Safari, but the filename appears as 'untitled'. Interestingly, it works fine in other browsers. var saveData = (function () { var base64 = "data:application/mswo ...

Should the page.js in a Next.js App router be a server component?

Is it standard practice in Next.js 13 and above (App router) for the page.js of all routes/folders to be a server component? I know that it can be a client component with use client, but is this advisable or are there potential issues during the build proc ...

PHP is returning an empty response during an AJAX request

I am facing an issue with my AJAX request where I am trying to return a simple echo, but for some reason, it's not working this time. Even after stripping down the code to its bare essentials, the response is still blank. Javascript function getUs ...

Deleting an element from an array stored in local storage with the help of jQuery

Summary: Developing a front-end wish list feature Tech Stack: Utilizing HTML5 (localStorage), CSS, and jQuery Key Features: Ability to add and delete items dynamically with real-time count display Challenge: Issue encountered when trying to remove added ...

Error sending an email through the EmailJS backend JavaScript file

I am in the process of creating a route for my backend JavaScript Express server called server.js that will trigger an email to be sent to my Gmail account using EmailJS. When I attempt to use the following code: const SMTPClient = require("emailjs& ...

React is throwing an error: Uncaught SyntaxError stating that import declarations are only allowed at the top level of a module. This issue is coming

I encountered an issue where I received the error message: Uncaught SyntaxError: import declarations may only appear at top level of a module on index.js sometimes en index.css or bundle.js Despite this error, my App is not rendering and the div with id=& ...

Decide whether to fulfill or deny a Promise at a later time in

When working on an Angular2/TypeScript project, a dialog is shown and the system returns a Promise object to the caller. This Promise will be resolved after the user closes the dialog. The interface of the Promise class does not include methods like resol ...

Updates made in the angular controller are not reflecting on the HTML page

I am facing an issue with displaying a "no internet access" button in my app when the user is offline and the required object is not available in the cache. Here is the HTML code: <div class="VideoPlayerController video-player-controller"> <span ...