Angular Bootstrap UI - Ensuring only one element collapses at a time

I have integrated the angular bootstrap UI library into my website by following this link: https://angular-ui.github.io/bootstrap/

One issue I am facing is that when I implement a collapsible feature using this library, it collapses or expands every element with the collapse functionality instead of just the specific one that is clicked.

Below is the code snippet I am currently using:

var app = angular.module('someApp', ['ui.bootstrap']);

app.controller('collapseController', ['$scope', function ($scope) {
  
   $scope.isCollapsed = true;
};
.passInfoDropdown {
  margin-top: 10px;
  margin-bottom: 10px;
  margin-left: 5px;
  font-weight: bold;
}

.dividerLine {
  background-color: #DED7CF;
  height: 2px;
}

.passInfoTableCellLeft {
  width: 220px;
}
<div class="dividerLine"></div>
<div class="click" ng-click="isCollapsed = !isCollapsed">
  <table>
    <tr>
      <td class="passInfoTableCellLeft"><div class="passInfoDropdown inline">TSA Information (optional)</div></td>
      <td><i class="fa fa-chevron-down inline"></i></td>
    </tr>
  </table>
  <div collapse="isCollapsed">
    <div class="well well-lg">Some content</div>
  </div>
</div>
<div class="dividerLine"></div>
<div class="click" ng-click="isCollapsed = !isCollapsed">
  <table>
    <tr>
      <td class="passInfoTableCellLeft"><div class="passInfoDropdown inline">Loyalty Programs (optional)</div></td>
      <td><i class="fa fa-chevron-down inline"></i></td>
    </tr>
  </table>
  <div collapse="isCollapsed">
    <div class="well well-lg">Some content</div>
  </div>
</div>

I need to modify the code so that only the unique element I click on collapses. How can I achieve this?

Answer №1

Currently, both variables isCollapsed are referencing the same property in your $scope object. If you wish to separate them, there are two possible solutions:

  1. Use two properties in the scope

    $scope.isCollapsedContent1 = true;
    $scope.isCollapsedContent2 = true;
    

    or

    $scope.isCollapsed = {};
    $scope.isCollapsed['content1'] = true;
    $scope.isCollapsed['content2'] = true;
    
  2. Implement two ng-models

If you do not require knowledge of whether the content is collapsed or not within your controller, consider placing the isCollapsed value directly into your HTML and removing the property from your $scope.

<div class="click" ng-click="isCollapsedContent1 = !isCollapsedContent1" ng-model="isCollapsedContent1" ng-init="isCollapsedContent1=true">

Answer №2

Hey Brandon, how are you? I noticed that in the complete code snippet, you seem to have missed out these elements...

<html ng-app="ui.bootstrap.demo">

and
<div ng-controller="CollapseDemoCtrl">

Take a look at the functional Fiddle.

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

Tips for utilizing identical properties across numerous styled elements:

Utilizing the styled-components library, I have enhanced the header components from the Blueprintjs library. The current template for the H6 component appears as follows: export const DH6 = styled(H6)` color: ${(props) => (props.white ? "white&qu ...

Getting data from a URL using Node.js and Express: A beginner's guide

Currently, I am facing an issue with extracting the token value from the URL http://localhost:3000/users/reset/e3b40d3e3550b35bc916a361d8487aefa30147c8. My get request successfully validates the token and redirects the user to a password reset screen. Howe ...

AngularJS caught in a module blockade

app.controller('TableContent', ['$http', '$scope', '$window', function ($scope, $window) { console.log("I'm in TableContent"); $scope.EditSaverCommit = function () { co ...

Tips for converting asynchronous function calls into synchronous functions in Node.js or JavaScript?

Imagine you are managing a library that provides access to a function called getData. Users utilize this function to retrieve real data: var output = getData(); In the background, the data is stored in a file, so you have implemented the getData functi ...

Exploring the Various Path Options in Angular 2 Routing

As a newcomer to Angular and Node JS, I am currently working on an application and struggling with how to efficiently navigate between my different components. Users can input the name of a user and add books associated with them When clicking on a book ...

I'm trying to figure out the best spot within this Mean.js application to insert the Angular code that will allow for sorting of

After receiving guidance from @Pavlo to utilize https://github.com/angular-ui/ui-sortable, I have a repetitive list that I am looking to make sortable. <div ui-sortable ng-model="regions" class="list-group region-list"> <a data-ng-repeat="re ...

Attempting to implement image switching with hover effects and clickable regions

Hey there, I'm currently working on a fun little project and could use some guidance on how to achieve a specific effect. The website in question is [redacted], and you can view the code I've used so far at [redacted]. You'll find a code blo ...

Arrange pictures into an array and showcase them

I'm encountering some difficulties with organizing my images in an array and displaying them in a canvas element. Javascript code snippet canvas = document.getElementById('slideshow'); canvasContent = canvas.getContext('2d'); va ...

Determine the RGB color values for specific coordinates within Adobe Illustrator

Currently exploring ExtendScript for JavaScript in Adobe Illustrator 2015. Is there a method to retrieve RGB values based on coordinates within the code below? // initializing document var doc = app.activeDocument; // defining x and y coordinates for colo ...

Learn the process of dynamically wrapping component content with HTML tags in Vue.js

Hey there! I'm looking to enclose the content of a component with a specific HTML tag, let's say button for this scenario. I have a function that dynamically returns a value which I use as a prop. Based on that, I want to wrap the component&apos ...

Nodemon has encountered an issue: Unable to listen on port 5000. The address is already in use

During the creation of my project with nodejs and express for the backend, everything was running smoothly. However, whenever I made changes to a file, nodemon would encounter an error preventing the server from restarting: Error: listen EADDRINUSE: addre ...

Why do users struggle to move between items displayed within the same component in Angular 16?

Lately, I've been immersed in developing a Single Page Application (SPA) using Angular 16, TypeScript, and The Movie Database (TMDB). During the implementation of a movies search feature, I encountered an unexpected issue. Within the app\servic ...

In Typescript, if at least one element in an array is not empty, the function should return false without utilizing iterators

My current approach involves receiving a string array and returning false if any of the elements in the array is false. myMethod(attrs: Array<String>) { for (const element of attrs) { if (!element) { return false; } } ...

Creating a unique carousel design using only CSS in Bootstrap

Trying to create a CSS effect to enhance Bootstrap, I noticed that it only works correctly when clicking the right arrow. Whenever I click the left one, nothing happens. I'm not sure where I made a mistake, can anyone help me spot it? Thanks in advanc ...

Retrieve isolated scope of directive from transcluded content

I am not certain if it is possible, but I am essentially looking for a reverse version of the '&' isolate scope in AngularJS. You can check out this Plunkr to see an example. In essence, I have created a custom directive that provides some r ...

Obtain the position and text string of the highlighted text

I am currently involved in a project using angular 5. The user will be able to select (highlight) text within a specific container, and I am attempting to retrieve the position of the selected text as well as the actual string itself. I want to display a s ...

Error: The object does not have the property createContext necessary to call React.createContext

I'm currently exploring the new React Context API in my app. I've also implemented flow for type checking. However, when I add // @flow to a file that contains the code: const MyContext = React.createContext() An error pops up stating: Cannot ...

Efficient initialization process in Vue.js components

Upon initialization of a component, the data callback is executed as follows: data(){ return { name: myNameGetter(), age: myAgeGetter(), // etc... } }, Following that, a notification is sent to a parent component regarding ...

Generating HTML tables with charts using FireFox

I am encountering an issue: My table contains charts and tables that are displayed correctly in browsers. However, when I attempt to print it (as a PDF) in Mozilla Firefox, the third speedometer gets cut off, showing only 2.5 speedometers. Using the "s ...

Using Lodash to Substitute a Value in an Array of Objects

Looking to update the values in an array of objects, specifically the created_at field with months like 'jan', 'Feb', etc.? One way is to loop through using map as demonstrated below. However, I'm curious if there's a more co ...