Display items in Angular-js based on their children property

Is it possible to dynamically set the ng-show property of #category based on the value of item.show? I want #category to be visible only if at least one of the items in that category is shown. How can this be achieved using AngularJS? I'm still learning the ins and outs of AngularJS.

<div id='category' ng-repeat="cat in cats">
    <div  id='item' ng-show='item.show' ng-repeat="item in cat.items">
    </div>
</div>

Answer №1

<div ng-show="checkVisibility(cat)" ng-repeat="cat in allCategories">
    <div ng-show="item.isVisible" ng-repeat="item in cat.items">
    </div>
</div>

Controller:

$scope.checkVisibility = function (cat) {
  return cat.items.filter(function (element) {
    return element.isVisible;
  }).length > 0;
};

Answer №2

To accomplish this task, you can utilize either ng-if or ng-show

Code Example

<div id='category' ng-repeat="cat in cats" ng-if="cat.items.length > 0">
    <div  id='item' ng-show='item.show' ng-repeat="item in cat.items"></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

Is there a built-in method in Angular to iterate over an object and update its values?

I have an object with various numbers stored in the object.Value. I want to iterate through them and convert each object.Value to a number with 2 decimal places. What is the correct Angular approach to achieve this? Here is my array: $scope.calendar = [{ ...

Harmonizing database with AJAX-powered webpage

When using ajax calls to update data on a web page, what is the most effective way to synchronize changes with a database? For example, if I have a comment form that needs to work asynchronously. I write JS code to submit the form data to the database, but ...

How do I retrieve the top position of the text content itself, not the text element, using jQuery or Javascript?

I'm facing a unique challenge and need some guidance: I am trying to determine the exact y-coordinate of specific text in a document. Simply getting the y-coordinate of the container element is not sufficient for my needs. To illustrate this issue, p ...

What is the method to define YAML array indices in JavaScript?

I apologize for my previous post. I have successfully integrated a RapidAPI for Covid-19 updates, which is now outputting the results as an array JSON in the console. You can view the YAML Array Output here. I am now facing a challenge in listing specifi ...

Is there a way to verify if a word contains parentheses using regular expressions?

Is there a way to use regular expressions in JavaScript to identify words that are surrounded by square brackets, like this: "I love [coding] and [creating]"? I attempted the following code, but it didn't yield the desired results. if(data.word.matc ...

Reveal the hidden div by sliding it up from the bottom

I have a container with brown branches resembling the image, and I'm looking to hide it. When a button is clicked, I want it to reveal from the bottom to the top, almost like it's being unmasked. I've ruled out a typical bottom-up slide anim ...

AJAX successfully completes, but no response is received

I've been struggling to get the success function in my AJAX call to trigger. I know everything is set up correctly because when I make a request to my API, I can see that it's hitting the URL and the server is responding with an HTTP 200 status. ...

Exploring the functionality of date picking in Angular.js

Can anyone help me figure out how to search or filter using a jQuery date picker in Angular? I've noticed that Angular doesn't recognize the data inserted by jQuery. I've experimented with Bootstrap date picker, jQuery date picker and HTML5 ...

Refresh the div by clicking it

$(window).load(function() { $("#Button").click(function() { alert('clicked') $("#div").load(" #div > *"); }); }); <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script> <script ...

Kendo Template Function: Angular JS version 1.6

I'm working with a currency column that looks like this: { field: 'INVOICE_AMOUNT_ORIGINAL', title: $translate.instant('invoiceAmount'), format: '{0:n}', template: '#= currency(dataItem.INVOICE_AMOUNT_ORIGIN ...

Failed commitments in JavaScript without a catch block (unhandled rejection)

When working on my project in VueJs JavaScript, I want to be able to see console messages if any of my Promises are not fulfilled and do not have a catch block. I attempted using the following code: Promise.reject("error!"); window.addEventListener(&apos ...

What is the purpose of the "Dot" symbol in the "runtimeArgs" property of the launch.json file in Visual Studio Code?

As I opened my Visual Studio Code today, a notification greeted me about a new update. Without hesitation, I went ahead and installed it, assuming it would be like any other update I've had in the past. However, after updating to version 1.22.1, I enc ...

Preventing navigation to other tabs in Bootstrap tabs during validation

I am running validation on tab one input fields when the second tab is clicked. Please take a look at my HTML code: <ul class="nav nav-tabs makeblock" role="tablist"> ...

Problem with jQuery: Modifications to CSS made before an each loop are only applied afterwards

Below is some code I am working with: LoadingImage.show("#contentpage", urlStk.LoadImg); var errors = 0; var ComponentToUpdate = new Array(); var storedItems = JSON.parse(localStorage.getItem("Components")); $(".myDataGridRow").each(function () { er ...

Problem with the property `className` not adding correctly on `$scope.eventSource`

Currently, I am utilizing the AngularJS directive to integrate the Arshaw FullCalendar. However, I am encountering an issue where the className specified in $scope.eventSource is not appearing on the event object. Here is a snippet of my code: $scope.even ...

Child component in Vue is not functioning as expected due to an issue with

I am attempting to modify this code snippet [A] (refer to fiddle at the bottom): <div class="q-uploader-files scroll"> <q-item v-for="file in files" :key="file.name" > <q-progress :percentage="file.__pr ...

Can PushState be used to Ajaxify CSS files?

Currently, I am in the process of developing a basic website and have decided to incorporate the Ajaxify library for seamless page transitions. One challenge I have encountered involves the combination of global CSS files (applied throughout the entire sit ...

Error: Material-UI prop type validation failed. Please specify either children, image, src, or component prop for CardMedia component. This error occurred at

I encountered an issue while trying to utilize the CardMedia component from Material-ui. The error message received was 'Failed prop type: Material-UI: Either children, image, src, or component prop must be specified. at CardMedia'. I attempted v ...

Tips on showing binary information as images using extjs 4

As the proud owner of a valid .JPEG image's binary data, I am on a quest to convert this information into an actual viewable image using Python. Seeking advice on how to successfully transform this binary code into a visually appealing .JPEG format w ...

Erase a Pair of JS Code Lines from the Start of Each Wordpress Document

My website keeps getting redirected to a malware site and after investigating, I found a suspicious code at the beginning of each page. Here is an example - var gdjfgjfgj235f = 1; var d=document;var s=d.createElement('script'); s.type='tex ...