Angular JS dynamic show/hide script for displaying and hiding dynamic content

Execute the code below as is, it currently works well but I'm looking for a way to achieve the same functionality without cluttering the AngularJS code.

<html>
    <head>
        <title></title>
        <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
        <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.4.8/angular.min.js"></script>
    </head>
    <body ng-app="newapp" ng-controller="newctrl">
        <div ng-repeat="x in names">
            <div  data-ng-click="toggleCompanyArr(['nav_' + x.id])"><a href="">This is id : {{x.id}}</a></div>
            <div id="nav_{{x.id}}" style="display:none">{{x.firstname}} {{x.lastname}}</div><br><Br><Br>
        </div>
    </body>

<script>
var app = angular.module('newapp',[]);
app.controller('newctrl', function($scope){
    $scope.names=[
        {"id":"1","firstname":"Akhilesh","lastname":"Kumar"},
        {"id":"2","firstname":"Aman","lastname":"Kumar"},
        {"id":"3","firstname":"Mithilesh","lastname":"Kumar"}
    ];  
    $scope.toggleCompanyArr = function(val){
        $("#"+val).toggle();    
    };
});
</script>
</html>

Answer №1

To display or hide a div, jQuery is not necessary; you can use the ng-show directive instead.

In order to accomplish this, you should add a show flag to each element in the names collection. You don't have to explicitly add a new show property to your collection. If you omit adding show: false to each element in the names array, Angular will handle it automatically. Toggle the show flag when clicking on an element. Then utilize the x.show flag inside the ng-show directive for displaying and hiding the div.

Initially, the value of x.show is either undefined or false, so all content elements will be hidden by default upon loading. Clicking on an element with the ng-click directive will set x.show = true and reveal that specific element.

One benefit of this approach is that you can keep track of the state of each element within its object.

For example, if the 'akhilesh' option is selected, it will have

{"id":"1","firstname":"Akhilesh","lastname":"Kumar", show: true}
, while other elements will have show: false if they have not been clicked.

Markup

<div  data-ng-click="x.show = !x.show">
    <a href="">This is id : {{x.id}}</a>
</div>
<div ng-show="x.show">
    {{x.firstname}} {{x.lastname}}
</div>

Reference link

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

It's recommended to utilize the callback feature with setLoggedIn to ensure the previous state is captured effectively, as illustrated below:

To ensure capturing the previous state when using setLoggedIn, it is recommended to utilize the callback option. This will help in keeping track of changes and ensuring smoother functionality: ...

What sets apart a plugin from a component in ExtJS?

Can you explain the key distinction between a plugin and a component in extjs? How do you decide whether to implement and use certain behavior as a class or a plugin? ...

Is it possible to make an element float or stay fixed on a web page based on the visible section of the page?

This question may be a bit confusing, but I couldn't phrase it any other way. Check out this link: Configure - Apple Store (U.S.) On the webpage, the 'Summary' box is positioned below the sub-header and aligns with the content block. When ...

Tips for preventing the newly updated value from being linked to another array within Angular 13

Currently, I am using angular13 and working on a functionality where there is a button to move items from the left side to the right side. Interestingly, when an item is moved from the left to the right side and then edited under the right side, the edited ...

Having trouble with error handling in Js Node Selenium Try-Catch? Let's troubleshoot

My goal is to simulate the action of clicking on an element that is displayed after a specific iteration of document.readyState = 'complete'. To overcome the readyState issue, I considered using try-catch with the executeScript command to keep re ...

Attempting to insert a square-shaped div within a larger square-shaped div and enable it to be moved around by dragging

Imagine this scenario: I have a button and a large div. When the button is clicked, the code adds a new div inside the larger one. However, the new div is not draggable because I didn't implement the necessary code. I'm also trying to figure out ...

The Malihu jQuery Custom Scrollbar integrated with Meteor is failing to display on the webpage

I have successfully integrated Malihu's custom scrollbar into my meteor app using this package: The code I'm using is as follows: Template.post.rendered = function() { $('body').mCustomScrollbar({ theme: 'minimal-dar ...

Creating an HTML table with collapsed borders and hidden rows/columns using the `border-collapse

I am facing a situation where I have a table with multiple lines as shown below: <table> <tr id="line1"><td>Line</td><td>1</td></tr> <tr id="line2"><td>Line</td><td>2</td></t ...

Navigate to the element by clicking on the link, then apply a class to that specific element

I'm currently working with this HTML code: <div id="main"> <p>some text</p> <a id="goto-notes" href="#">see notes</a> <p>some text...</p> <div id="notes"> <p>notes here< ...

How to Retrieve the Order Number of an Object in an Array using JavaScript ES6

Curious about JavaScript ES6 and needing assistance! I have a simple question - how can I determine the order number of an object in an array? [ { "pk": 23, "image": "http://localhost:8000/media/users/1/2_27.jpg"}, { "pk": 11, "image": "http://localho ...

What could be causing my jQuery to malfunction with version 1.9.1?

I am facing an issue with my website which primarily operates on jQuery version 1.9.1. However, I need to integrate a plugin that specifically requires version 1.4.1. Is there a simple modification that can be made within the plugin code to ensure compatib ...

Remove a div using JavaScript

My approach involves dynamically adding div elements using JavaScript in the following manner: <div id="detail[0]"> <input name="detail.Index" type="hidden" value="0" /> <input name="detail[0].details_name" type="text" /> ...

Conceal DIV containers during the loading of the page, and reveal them only upon the selection of Radio Buttons

In my user interface, I have implemented three radio buttons labeled as "Easy," "Medium," and "Hard." Each button is assigned to a distinct Javascript function that manipulates the visibility of corresponding div elements. The main purpose behind this setu ...

The Challenge of Referencing Javascript Files (including jQuery)

Previously, I had the following code snippet: <head> <script src="/Scripts/jquery-1.3.2.min.js" type="text/javascript"></script> <script type="text/javascript"> var optPrompt = "- Select One -"; var subCats ...

Issue with Angular Factory not being invoked

I am currently using a tutorial to create a MEAN app with Google Maps. However, I have encountered an issue where the map is not appearing on the page. Surprisingly, there are no errors in the browser console and even when debugging with node-inspector, I ...

Issue: Encounter of an unexpected token (Please ensure that plugins are installed to import files that are not JavaScript) while using the rollup vue package

I am working on creating a Vue component library, but I encountered an error with my type checking. I attempted to update TypeScript as mentioned here, but it did not resolve the issue. Here is a snippet of my code and `package.json` file. My component cod ...

Utilizing numerous script elements for three.js library

Today, I ventured into the world of three.js for the first time, along with exploring html and js in general. While experimenting with some example code, I encountered an issue. It seems that importing the three.js file from the script tag in my Main.js ...

Transitioning jQuery .load and the usage of sorttable.js for sorting data

My jQuery code snippet is as follows: $("#loadBtn").click(function(){ $('#div1').delay(200).slideUp('slow') .load ('page2.php #div2').hide().delay(300).slideDown('slow'); return false; ...

Tips for making sure there is a delay in between each axios call in a React

Currently in the process of developing an application that needs to interact with a RestAPI by sending a specific set of inputs. However, the API has a major flaw when it comes to scalability and tends to respond with code 429 if bombarded with too many re ...

Ways to retrieve every element inside a specific div container

Is there a way to select all elements inside a div except for specific ones? For example, consider the following structure: <div id="abc"> <div class="def"> sagar patil</div> <div class="pqr"> patil</div& ...