Angular UI Bootstrap collapse directive fails to trigger expandDone() function

I am currently utilizing UI Bootstrap for Angular in one of my projects, and I have developed a directive that encapsulates the collapse functionality from UI Bootstrap. Here is how it looks:

app.directive( 'arSection', ['$timeout', function($timeout) {
   return {
      template: '<div class="section" ng-class="{\'collapsed\': collapsed, \'open\': !collapsed}">' +
                '    <h4 ng-click="toggleCollapsed()">' +
                '        <span class="dropdown-ctrl" ng-class="{\'icon-chevron-right\': collapsed, \'icon-chevron-down\': !collapsed}"></span>{{title}}' +
                '    </h4>' +
                '    <div collapse="collapsed" class="section-content">' +
                '        <div ng-transclude></div>' +
                '        <div class="clear"></div>' +
                '    </div>' +
                '</div>',
      restrict: 'E',
      scope: true,
      transclude: true,
      replace: true,
      link: function( scope, elem, attrs ){
         scope.title = attrs.title;

          // Toggle the open/closed state and save it away
          scope.toggleCollapsed = function() {
              scope.collapsed = !scope.collapsed;
          };

          scope.collapsed = true;
         }
      }
   };
} ] );

The issue I am facing is that when expanding the div, the expandDone() method in UI Bootstrap is not being triggered upon completion of the animation.

function expandDone() {
  element.removeClass('collapsing');
  element.addClass('collapse in');
  element.css({height: 'auto'});
}

As a result, the height of the div does not adjust dynamically with the content inside, causing some content to be cut off.

In addition, the collapseDone() method also fails to execute during collapsing.

Interestingly, these methods are only triggered after interacting with another component in the application, which indirectly initiates an apply/digest cycle for the collapse directive and executes the queued callbacks.

I suspect that this behavior is related to Angular scopes or watches, but I am unable to identify the specific connection causing this delay.

UPDATE: I have created a Plunker showcasing the arSection directive, where it functions correctly. Clicking on Section One header triggers the animation to expand. However, in my actual project, the Section Two header does not reposition itself as expected when the content in Section One expands. The overflow of larger content remains unadjusted.

If you inspect the HTML during runtime, you will notice the "collapsing" class being added and removed during animation. While the "collapsing" class is applied in my code, it is not removed until a trigger event forces an apply/digest cycle.

Answer №1

In this scenario, Angular UI's accordion feature could be a suitable option. However, for the purpose of learning, here is an alternative approach that works: Plunker

By using ng-init and ng-click, you can simplify the code and eliminate the need for a controller logic or directives.

<div class="container" ng-controller="CollapseDemoCtrl">
    <h3>Click on the section headers to expand them</h3>
    <hr>
    <div class="col-md-12">
        <h3 class="" ng-init="showContentOne=false" ng-click="showContentOne=!showContentOne">
        <i ng-class="showContentOne ? 'fa fa-angle-down' : 'fa fa-angle-right'"></i> Show Content One</h3>
        <div ng-show="showContentOne" class="well well-lg" ><h5>Lots of content for the first section</h5></div>
    </div>
    <div class="col-md-12">
        <h3 class="" ng-init="showContentTwo=false" ng-click="showContentTwo=!showContentTwo">
        <i ng-class="showContentTwo ? 'fa fa-angle-down' : 'fa fa-angle-right'"></i> Show Content Two</h3>
        <div ng-show="showContentTwo" class="well well-lg" ><h5>Additional content for the second section!</h5></div>

    </div>
</div>

Answer №2

Upon investigation into why my images were not automatically adjusting to different screen sizes, I came across the directive and discovered that resizeImage() was never being executed.

To resolve the issue, I ended up including ngAnimate and making it a dependency in my code:

angular.module('website', ['ngAnimate']);

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

Problem with the getJSON function

Here is a question that has been bothering me: I am currently working on a barcode scanner script which retrieves data from a JSON file. The script itself functions properly, except for one issue. After the while loop, I want to display an error alert m ...

"Encountering a Node.js error while trying to open an image for the second

My goal is to use Node to take a screenshot of the user's screen on all displays and save everything in a single image. Everything works fine when I call it for the first time, but if I try to call it a second time without restarting the server, an er ...

Having trouble retrieving information from .pipe(map()) method

Encountering some issues with Observable handling. I have a function that returns an Observable. public getData(userId) { const data = this.execute({userId: userId}); return {event: "data.get", data: data} } private execute(input: SomeDt ...

Guide on utilizing multiple ng-apps alongside multiple controllers

Having trouble accessing controller values in different ng-apps? Here's a setup with two ng-apps and their controllers, where you may encounter errors when trying to access the value of one controller in another. Need some assistance with this issue. ...

Interactive carousel featuring responsive image zoom effect on hover

Utilizing the flickity carousel, I have crafted an example which can be found at this link to codepen.io. Here is the CSS code that has been implemented: CSS .image-hoover { overflow: hidden; } .image-hoover img { -moz-transform: scale(1.02); -web ...

Creating a personalized directive for post-submit data validation in AngularJS

Looking to implement a custom directive for data validation post web service call. Stumbled upon a blog example; however, it's triggering the web service call on every value change in the form! Desire for the custom directive to only activate upon us ...

Effective ways to transmit a variable array from an HTML document to a PHP server

Is there a better method for transferring a variable array from HTML to PHP? I attempted to use the serialize function, but it doesn't seem to be functioning correctly. Any suggestions would be greatly appreciated. //HTML var arrayTextAreasNames = [ ...

Looking for a new slider option to replace the traditional Conveyor belt slideshow?

I have successfully used the Conveyor belt slideshow script from http://www.dynamicdrive.com/dynamicindex14/leftrightslide.htm. Now, I am looking to find another script that works similar to this one. Does anyone have any recommendations for a tool that ...

Can you explain how to utilize a function on a client component in Next.js?

I'm a bit lost on how client components function. I am working on an image uploader project where I need to extract the userId from supabase, utilize the supabase server function, and then upload the image to supabase storage with the "userId/filename ...

What is the prescribed interface or datatype for symbol type in TypeScript with JavaScript?

I have a set of symbol values in JavaScript that I want to convert to TypeScript. // Defining object values in JavaScript const size = { Large: Symbol('large'), Medium: Symbol('medium') } What is the most efficient method to conv ...

Subscribing with multiple parameters in RxJS

I am facing a dilemma with two observables that I need to combine and use in subscribe, where I want the flexibility to either use both arguments or only one. I have experimented with .ForkJoin, .merge, .concat but haven't been able to achieve the des ...

Despite the headers being in place, the node is still the point of

I am facing an issue with two URLs residing on the same server, mydomain.com and api.mydomain.com In order to handle access-origin in my API, I have included the following code snippet: app.use(function (req, res, next) { // CORS headers res.head ...

AngularJS Element Connections

I have the following component: (function () { "use strict"; angular.module("application_module") .component('tab', { controller: 'TabCtrl', templateUrl: 'app/component/application/app-heade ...

How to Safeguard an AJAX Service Request using jQuery and PHP (with a Session Token)

Currently, I am developing a framework where form submissions are handled through jQuery ajax calls to a .php service file. Here is a snippet of the jQuery code for reference: var dataSerialized = $(form).serialize(); var service = $(form).attr("action") ...

"Upon refreshing, the background image reverts back to its original default setting

With React, I have implemented a feature where users can navigate to their 'settings' page and select an image to set as their background. I used DOM manipulation to achieve this functionality successfully! However, whenever the page is refreshed ...

The storage functionality in this session is malfunctioning

Take a look at this code: <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="WebApplication3.Default" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> < ...

Saving changes to mesh vertices in r67 of Three.js

I've encountered an issue with saving a mesh to a text file after manipulating vertices in my plane model. While the rendering on screen works as expected, the updates are not reflected in the saved file. Interestingly, if I move a vertex before the ...

Discovering ways to align specific attributes of objects or target specific components within arrays

I am trying to compare objects with specific properties or arrays with certain elements using the following code snippet: However, I encountered a compilation error. Can anyone help me troubleshoot this issue? type Pos = [number, number] type STAR = &quo ...

There seems to be a problem with the data-binding functionality in the AngularJS code through

My issue arises when I attempt to update the default values in the textArea after clicking the button. The changes made to the text (firstName/middleName/lastName) fields do not reflect upon clicking the button. The code related to this problem is spread ...

Utilize AxiosAbstraction to transmit a Patch request from a Vue.js application to a PHP backend

I need help with sending a PATCH request to update the birthdate of a user (promotor) from Vue.js frontend to PHP backend. The issue I'm facing is that the new date of birth is not getting saved in the database, and the existing date of birth in the d ...