AngularJS Foldable Menu

Is it possible to expand an accordion panel on button click using AngularJS?

Below is the HTML code example:

<!doctype html>
<html ng-app="plunker">
  <head>
    <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.0.5/angular.js"></script>
    <script src="http://angular-ui.github.com/bootstrap/ui-bootstrap-tpls-0.2.0.js"></script>
    <script src="example.js"></script>
    <link href="//netdna.bootstrapcdn.com/twitter-bootstrap/2.3.1/css/bootstrap-combined.min.css" rel="stylesheet">
  </head>
  <body>

<div ng-controller="AccordionDemoCtrl">
  <accordion>
    <accordion-group ng-repeat="group in groups"  is-open="heading1.isOpen">
      <accordion-heading>
        <span ng-click="opened(group, $index)">{{group.title}}</span>
      </accordion-heading>
      {{group.content}}
    </accordion-group>    
    <button class="btn btn-default btn-sm" ng-click="heading1.isOpen = !heading1.isOpen">Toggle 1</button>
  </accordion>  


</div>


  </body>
</html>

The 'example.js' script defines the functionality for expanding and collapsing the accordion sections based on user interaction.

By utilizing AngularJS, angular-ui, and Twitter Bootstrap, it is indeed possible to have a single accordion that expands dynamically using ng-repeat. However, issues may arise with button clicks not functioning correctly when implementing ng-repeat.

Answer №1

  • Connect it to group.open instead of heading1.isOpen (is-open="group.open")
  • Simply set group.open = true when clicked

By utilizing two-way binding, the group will automatically expand. No need to manipulate the HTML, just interact with the data and let angular/ui.bootstrap handle the rest through binding.

Answer №2

It's unclear where the heading1 variable originated, but it seems to be causing the problem at hand.

My suggestion is to switch that out with group and modify isOpen to simply open as per the definition in your groups array.

<accordion>
    <accordion-group ng-repeat="group in groups"  is-open="group.open">
        <accordion-heading>
            <span ng-click="opened(group, $index)">{{$index+1. + " "+ group.title}}</span>
        </accordion-heading>
        {{group.content}}
    </accordion-group>
    <div ng-repeat="group in groups">
        <button class="btn btn-default btn-sm" ng-click="group.open = !group.open">Toggle {{$index+1}}</button>
    </div>
</accordion>

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

Could you provide steps on incorporating a Qt native window into an Electron Project?

Can Qt be integrated into an electron application? I would like to incorporate a 3D viewer developed in Qt/C++ into my electron app, but it is currently not compatible with electron/node. Any other suggestions would be appreciated. Feel free to provide ot ...

Error in server file of NextJS due to Typescript issue

My current setup includes the following package versions: next v8.1.1-canary.42 @types/next v8.0.5 server.js import next from 'next'; const app = next({ dev: isDevelopment }); tsconfig.json { "compilerOptions": { "allowJs": true, ...

Dealing with the problem of delayed image loading in AngularJS due to a setTimeout conflict

The issue: Upon page load, some images are still processing and not displaying (even though the image URLs are known). The resolution: To address this problem, a customized directive was developed to showcase a loading spinner as a placeholder until the i ...

Determine with jQuery whether a URL already contains a query string

When the site is loaded for the first time and a user selects a hospital location from the dropdown menu, the URL should have hos=somelocation as a query string parameter. If the URL already contains other query string parameters like then I need to chec ...

Angular-Material - Issue related to Theme Implementation

Recently, I discovered Angular-Material. However, it appears that the theme files are not automatically included by the base angular-material.css file. As a result, I have to manually include each color theme that I want to use... If anyone has dealt wit ...

Alter the uploaded picture and subsequently send the revised image through an HTML form

Currently, I am working on an HTML form that includes a file input for accepting images. My goal is to take the uploaded image from the user and make some modifications using JavaScript before submitting the modified version instead of the original one. I ...

Tips for refreshing a list in Angular using a service after form submission

As a beginner with Angular, I am setting up a simple "submit comment" and "display list of comments" page. My goal is to automatically update the list of comments after submitting a new one, without having to manually refresh the page. Here are my control ...

Why do variables maintain the same value across modules when using the require function in Node.js?

We have a scenario where we have multiple files in the same directory: data.js var anArray = [1,2]; module.exports = anArray; mod1.js var data = require('./data'); module.exports = data; mod2.js var data = require('./data'); modul ...

How to access a variable within a callback function in Node.js

Is there a way for me to retrieve the inventory variable outside of the callback function in order to use it and return its value? loadInventory = function () { var inventory = []; offers.loadMyInventory({ appId: 730, contextId: 2, tradabl ...

Angular button press

Recently, I started learning Angular and came across a challenge that I need help with. Here is the scenario: <button *ngIf="entryControlEnabled && !gateOpen" class="bottomButton red" (click)="openGate()">Open</button> <button *ngIf ...

Can HTML5 and JavaScript be used to clip a portion of a video and upload it directly to a server?

Currently, I am using Filereader to read a local video file (mp4) in order to display it in a video tag. However, I am looking to cut a specific part of the mp4 file (for example, from 5 to 10 seconds) and then upload it to a server. My current approach: ...

Displaying JSON data dynamically by iterating through it in a loop

While working with JSON data in a loop, I noticed that the code quality does not meet my expectations. I have a feeling that I might be missing something in my approach. $(function(){ $.getJSON('data.json', function(data){ let content ...

The relocation of the route from app.js to route.js has caused a malfunction in the app

After restructuring my code, I encountered an issue with a route that was working fine before. Initially, the code was in my app.js file and it worked as intended. However, after moving it to its own route at routes/random, I started receiving a "http://lo ...

Errors found in Puppeteer click function

Can someone help me figure out why Puppeteer is not clicking on an element for me? Here is the code I am using: var clickPhone = '//*[contains(@class, "ep-epage-sidebar__phone-button")]'; var showPhone = '//*[contains(@class, ...

Accessing XML content within a web browser

I am facing an issue with parsing a string in JavaScript. var output = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><abc><xyz><xyzResponse><URL>http%3A%2F%2Flocalhost%3A8080%2Fnet%2Fxyz.do%3Fpartner%3Ddummy%26 ...

Utilizing React to bind "this" to a method within a class

Currently, I am immersed in a React book that suggests binding methods like this: this.onClickMe = this.onClickMe.bind(this); However, the functionality seems to work perfectly fine without including the above code snippet. class ExplainBindingsComponen ...

Tips for displaying personalized error messages from your REST API in a JavaScript client

I'm utilizing a Java REST API that has been generated using swagger. If the client is unauthorized, I am sending custom error messages in response. public Response collaborationCollabIdDelete(Integer collabId, SecurityContext securityContext, Str ...

Function reference in JSDoc that is not within a class context

If I have two stand-alone functions in the structure outlined below: A/foo.ts B/bar.ts Where bar.ts contains export const happy()... And foo.ts contains /** @see happy /* How can I establish the correct linkage to bar#happy? I experimented with borr ...

How is it that this JavaScript task does not trigger an error: const a = (1, 2, 3, 4);

let x = (5, 6, 7, 8); console.log(x); let y = 5, 6, 7, 8; console.log(y); In the example above, x will be assigned a value of 8, while the second line will result in an error. What is the reason behind the success of the first assignment? How does it qua ...

Utilizing an Angular foreach loop for restructuring JSON data

I currently have an ng-repeat function that outputs arrays of objects in the following format: [ {"day":"10","title":"day","summary":"summary","description":"ok","_id":"53f25185bffedb83d8348b22"}, {"day":"3","title":"day","summary":"summary","description" ...