Clicking on items within a list to toggle the display of a specific section using AngularJS directives

I'm brand new to AngularJS and I'm attempting to create a row with 3 elements per row in an ng-repeat. When the user clicks on one of the row elements, another row should appear below it based on the ng-show I have set up. I organized each row into different objects and placed the show between the lines. My question is, can I use ng-click in one row to hide the row that may have been clicked in another row?

My idea is to ng-click the selected element and display its information. Anything on the same row is fine because they will all bind to the same ng-show for the row. The issue arises when I click on one element in one row and then click on another element in the second row, both divs show up, but I only want the one I clicked on to show.

Any suggestions? I've tried a lot of things and have made some progress, but haven't achieved the exact user experience I'm aiming for. Thank you, and apologies for being inexperienced. It's great that I can come here for help since no one at my company has experience with Angular.

<div ng-controller="oneCtrl">
    <div class="row">
        <div ng-repeat="image in images" class="span4 padding-bottom30">
            <a href rollover ng-click="select(image)">
                <img class="img-responsive padding-top30" ng-src={{image.img}}/>
            </a>
        </div>
    </div>
    <div ng-show=selected>
        <div class="row">
            <div class="span4 padding-top30">
                <h3 class="mont margin-top0">{{selected.name}}</h3>
                <p><b>Mission Statement</b></p>
                <p>{{selected.info}}</p>
                <a href={{selected.site}}><button class="btn btn-colour-red pull-left">VISIT SITE</button></a>
            </div>
            <div class="span8 padding-top30">
                <img class="img-responsive" ng-src={{selected.photo}}/>
            </div>
        </div>
    </div>
    <div class="row">
        <div ng-repeat="pics in images" class="span4 padding-bottom30">
            <a href ng-click="select(pics)">
                <img class="img-responsive padding-top30" ng-src={{pics.img}}/>
            </a>
        </div>
    </div>
    <div ng-show=selected>
        <div class="row">
            <div class="span4 padding-top30">
                <h3 class="mont margin-top0">{{selected.name}}</h3>
                <p><b>Mission Statement</b></p>
                <p>{{selected.info}}</p>
                <a href={{selected.site}}><button class="btn btn-colour-red pull-left">VISIT SITE</button></a>
            </div>
            <div class="span8 padding-top30">
                <img class="img-responsive" ng-src={{selected.photo}}/>
            </div>
        </div>
    </div>
</div>

Answer №1

Utilizing ng-show="selected" in both of the DIVs within the section is causing both to be displayed. This variable controls whether the sections are hidden or shown when clicking on an image. To resolve this issue, it is recommended to use two separate variables, such as selected1 and selected2, and adjust them based on which image is clicked (row 1 or row 2).

For more information, please refer to this plunker.

I trust that this explanation will be beneficial.

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

The Vue3 property 'x' is not recognized on type 'Function' as a global property

I have encountered a strange issue with my Quasar app. I am attempting to define a global variable that consists of metadata about the application in an object format. Despite successfully compiling the app and displaying the correct information on the HTM ...

Utilizing Parameters in Route Mapping within Node.js API

Currently, I am in the process of setting up an API for my website. However, I am encountering some difficulties with the route params. At the moment, I have implemented two routes: const route1 = require('./routes/route1') const route2 = requir ...

What is the method for transmitting a message from localhost to a React frontend?

I am currently running a WebSocket server that is receiving streams of messages from an MQTT source. These messages are then sent to a localhost server on port 8080. The messages being received are actually a stream of random numbers. Below is the Python ...

Instead of receiving a response, an error is being displayed in the terminal

I'm currently developing a RESTful API using the Express framework. I've implemented a middleware, but for some reason, the error isn't being passed to the middleware as expected. I have code in the following files: in router.js router.get(& ...

The '$element' in an AngularJS directive is essentially treated as a comment when using ng-if

I am currently working on creating a popup (dialog) directive for my angularjs application. There are still many things to do, but I have already created a template file that constructs the popup element and inserts values from the attributes passed with t ...

Custom geometry in Three.js isn't responding to lighting as expected

My code is set up to create a unique diamond shape in Three.js: var material = new THREE.MeshPhongMaterial({color: 0x55B663, side:THREE.DoubleSide}); var geometry = new THREE.Geometry(); geometry.vertices.push(new THREE.Vector3(0, 1, 0)); geometry.v ...

Another problem with CORS again?

My rails application uses the host http://myhost.dev to search for music through the vk.com API. The API provides a search method called audio.search. Below is the code snippet from my search.js.erb file which is executed via a remote link to the search ac ...

Tips for including markers on Google Maps once the map has finished loading:

Currently, I am developing a phoneGap application and going around my house to gather coordinates: var locations = [ {lat:123.12313123, lng:123.123345131}, {lat:123.12313123, lng:123.123345131} ] While referring to the documentation here, everything ...

Error code 405 (METHOD NOT ALLOWED) is received when attempting to make a post request to an API

Struggling to develop a basic front end that can communicate with my API. The API functions properly as I am able to retrieve and send data using the POSTMAN client. Fetching data from the server and displaying it on the frontend works fine, but encounteri ...

Is there a way to extract all the titles from a json file that have a specific word in them?

Here is a function that returns props to an element named box in React. The MovieDatabase is a JSON file that contains titles of various movies. I am interested in displaying only the movies whose title includes "Star Wars". const showMovies = MovieData ...

Retrieving the background image from the main page

Struggling to locate the background image on this website. Even after using Chrome dev tools and viewing the source, I can't seem to find it. It's like they are hiding the link to the background image very well. Any assistance in finding it would ...

Avoiding GulpJs freezing with effective error handling techniques

I am currently in the process of converting my sass files to css and encountering some issues. Specifically, I am struggling with handling errors during the sass conversion process. Whenever there is an error, it causes gulp to hang, requiring me to rest ...

What is the process for assigning an item from the result list to the parent Div tag of the current object?

I've been working on a function to insert the number of Facebook Likes into a Div tag. The script I have so far can extract the URL from a Div tag inside another Div named 'entry'. Then, using the .getJSON() method, it fetches the Facebook l ...

Dynamic Array Names in JSON Datatables

I'm currently working on integrating the JSON data below into a Datatable. The issue I'm facing is that each stock's data is stored in its own array and the names of these arrays (tickers) can vary dynamically. In this specific example, ther ...

Retrieving a URL from an Angular JS link with the help of Selenium WebDriver using Python

My goal is to extract the URL from a "Visit Website" link located at The source code snippet looks like this: <span class="text-frame" ng-class="(vm.getHaveSecondaryWebsites()==true) ? 'with-aditional-item':''"> <span ng- ...

Is it possible to synchronize functions in node.js with postgresql?

I’m facing some challenges in managing asynchronous functions. Here is the code snippet that's causing the issue: var query = client.query("select * from usuario"); query.on('row', function(user) { var queryInterest = client. ...

Insert an element into a JSON collection

I am currently working on a JavaScript function that utilizes an ajax call to retrieve data from an API in the form of a JSON array. Here is a snippet of the array structure that I receive: [ { "ErrorType": "Errors", "Explanations": [ { ...

I recently discovered how to modify the video source (src) within an html5 source tag, but I am encountering varied outcomes when using it within a function or with inline javascript

When attempting to change the src of a video tag using Javascript, I encountered an issue. The code works fine when placed inline, but as soon as I try to include it within a function or in the "onclick" event of a button tag, nothing happens. There are no ...

Adjusting the size of Bootstrap alerts while ensuring the close icon remains correctly positioned

Below is the HTML code snippet I am working with: <div class="row mt-2"> <div class="col-lg-5"> <div id="alertmessages"></div> </div> <div class="col-lg-7"> <div class="btn-group-sm"> ...

Cookie fails to transmit upon deploying my application on Vercel

After developing a straightforward authentication system using cookies and expressjs, everything was running smoothly on my local environment. However, upon deployment to Vercel, I encountered issues where the program code was not executing properly (cooki ...