Angular JS has the capability to toggle the visibility of elements on a per-item basis as well as

I have created a repeater that displays a headline and description for each item. I implemented a checkbox to hide all descriptions at once, which worked perfectly. However, I also wanted to allow users to hide or show each description individually. I almost have it working, but there's one issue: if I hide all descriptions with the checkbox and then try to show one by clicking on it, nothing happens until I click it a second time.

Is there a workaround for this problem?

Below is the code I am using:

<div id="container" ng-app="" ng-controller="myController">
    <input type="checkbox" ng-model="MinAllDescriptions" /> <span>Minimize all descriptions</span><br /><br />

    <div class="itemContainer" ng-repeat="item in ItemList">
        <span class="itemHeadline">{{item.Headline}}</span> 
        <a href="#" ng-click="MinThisDescription = !MinThisDescription">Hide / Show</a>
        <div class="itemDescription" ng-hide="MinAllDescriptions || MinThisDescription" ng-show="!MinAllDescriptions || !MinThisDescription">{{item.Description}}</div>

    </div>
</div>

<script>
function myController($scope) {
    $scope.MinAllDescriptions = false;
    $scope.ItemList = [
        {Headline: "Item one", Description: "This is item one"},
        {Headline: "Item two", Description: "This is item two"},
        {Headline: "Item three", Description: "This is item three"},
        {Headline: "Item four", Description: "This is item four"},
        {Headline: "Item five", Description: "This is item five"}
    ];
}
</script>

You can view the interactive version on jsfiddle here: http://jsfiddle.net/195k2e9n/1/

Answer №1

For a solution, check out this JSFiddle link

Here is the HTML code:

<div id="container" ng-app="" ng-controller="myController">
    <input type="checkbox" ng-click="minimizeAll()" ng-model="MinAllDescriptions" /> <span>Minimize all descriptions</span><br /><br />

    <div class="itemContainer" ng-repeat="item in ItemList">
        <span class="itemHeadline">{{item.Headline}}</span> 
        <a href="#" ng-click="item.MinThisDescription = !item.MinThisDescription">Hide / Show</a>
        <div class="itemDescription" ng-hide="item.MinThisDescription">{{item.Description}}</div>            
    </div>
</div>

And here is the Angular script:

function myController($scope) {
    $scope.MinAllDescriptions = false;
    $scope.ItemList = [
        {Headline: "Item one", Description: "This is item one"},
        {Headline: "Item two", Description: "This is item two"},
        {Headline: "Item three", Description: "This is item three"},
        {Headline: "Item four", Description: "This is item four"},
        {Headline: "Item five", Description: "This is item five"}
    ];

    $scope.minimizeAll = function(){
        angular.forEach($scope.ItemList, function(item, i){
            item.MinThisDescription = !$scope.MinAllDescriptions;
        });
    }
}

Answer №2

Checkbox event can be added to toggle the value of $scope.MinAllDescriptions variable

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

Creating a fresh JSON structure by utilizing an established one

I have a JSON data that contains sections and rubrics, but I only need the items for a new listing. The new object named 'items' should consist of an array of all the items. The final JSON output should be sorted by the attribute 'name&apos ...

Generating a JavaScript array in PHP

As I work on developing a dynamic Google Geochart, one key aspect is creating an array to hold the data. The structure includes the country as the unique identifier and the color value to determine the map shading. arrayData = [['Country',' ...

New post: "Exploring the latest features in Angular

Looking for help with integrating Angular and SpringREST to fetch data from the backend? Here's my situation: I need to retrieve a JSON string from the backend using a POST request, send it to my site's hosted link, and display it on the user int ...

The integration of express and cors() is malfunctioning

Currently, I am developing a React application and facing an issue while trying to make an API call to https://itunes.apple.com/search?term=jack+johnson In my project, there is a helper file named requestHelper.js with the following content : import &apo ...

A warning message has been triggered: 'Script Alert Error - Object

I've been putting in hours on a SUP project that requires some tweaking and I keep running into the issue Script Alert Error: Object expected The code I've added is: $('#bottomOfStart_ScreenForm').before('<div style="display: ...

How can we determine the total character count of a file that has been loaded into a textarea

I have a textarea where I can count the number of characters as I type. function calculateCharacters(obj){ document.getElementById('numberCount').innerHTML = obj.value.length; } <textarea name="textField" id="my_textarea" class="text_edit ...

Using Javascript to parse SOAP responses

Currently, I am working on a Meteor application that requires data consumption from both REST and SOAP APIs. The SOAP service is accessed using the soap package, which functions properly. However, I am facing challenges with the format of the returned data ...

Tips for extracting a value from a currently active list item's anchor tag with JQuery on Mapbox API?

Currently, I am attempting to extract the value from a forward geocoder that predicts addresses while a user is typing. My goal is to then send this value to a form with an id of "pickup". However, I am encountering difficulties in capturing the li > a ...

Exploring the distinctions between ajax, await, async, and

English is not my strong suit, so please bear with me if my writing seems odd. We have discovered two methods for transitioning from asynchronous ajax calls to synchronous ones. Using async: false Utilizing await Both achieve the same outcome, but I am ...

Issue with Videogular: hh:mm:ss Date filter not functioning properly

I am currently working on integrating Videogular into my audio player application. Below are the settings provided in the example code on this particular page: <vg-time-display>{{ currentTime | date:'mm:ss' }}</vg-time-display> ...

How to include a javascript file in a different file and compile it using Node.js

For running my practice JS files, I rely on Node. As an example, I use node bts.js. In order to implement a queue, I decided to install Collections by using the command npm install collections --save. You can also see this reflected in the hierarchy shown ...

`enable cookie sharing between subdomains with the combination of express and angularjs`

I've been struggling with a task for quite some time now without any success. I would greatly appreciate it if someone could assist me with this. Present Scenario: I have two applications running on two sub-domains: st.localhost:8080 and acm.loca ...

Having issues with accessing data from Informix database in PHP due to an undefined index?

I am encountering the following error multiple times per row: Notice: Undefined index: enviopre in /opt/lampp/htdocs/pruebax/pruebaxone.php on line 34 Notice: Undefined index: enviofra in /opt/lampp/htdocs/pruebax/pruebaxone.php on line 35 Notice: Undef ...

Despite my usage of className, I still encounter the error message "Error: Invalid DOM property `class`."

I am having trouble debugging this issue as I am unsure of the exact location of the error. Here is the link to the repository: https://github.com/kstaver/React-Portfolio Error #2. There are three more promise rejection errors present, which I will addres ...

Is there a problem with the way divs are being displayed when using jQuery to show the first 12 elements with the class "hide-show"?

I am encountering a problem with displaying data using the jQuery slice() and show() methods to reveal dynamically generated divs from a PHP function. The code is as follows: <style> .hide-show { display :none; } </style> <div class="c ...

NG6002 error: This error is showing up in the imports of AppModule, even though it has its own set of issues

Running Angular 12 locally is smooth with no errors in the project build. Local Configuration: Angular CLI: 12.0.5 Node: 12.16.3 Package Manager: npm 6.14.4 OS: win32 x64 Angular: 12.0.5 However, when attempting to build the project on a Linux se ...

Issues with JQuery `.click()` event

Check out this snippet of code I'm working with: $(".item").click(function () { alert("clicked!"); }); I also have (hypothetically; in reality it's more complex) the following HTML on my page: <a href="#" class="item"> ...

Convert this text into HTML code: "Textarea to

I have a basic <textarea> element that I want to transform links (www.google.com, msn.com, etc) and line breaks (\r\n) into HTML code. I found one library for converting links into <a hrefs>. Another library can convert line breaks i ...

Using JQuery to duplicate and assign individual IDs to each clone

Currently, I am in the process of developing a straightforward web application. Users will enter information by selecting options from drop-down menus and typing text into designated fields. Upon completion, users can click on a "generate" button that will ...

Encountered an EACCESS error while attempting to generate package.json in Angular

Upon completing the command line "yo angular" and following all the necessary steps, I encountered this error : Screenshot of the error I attempted to run it using "sudo yo angular" but unfortunately, it did not resolve the problem. Does anyone have any ...