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

Is it possible to change between universal and spa mode based on the query string?

Currently, I am working on a Nuxt.js application set to universal mode and deployed as a static website using nuxt generate. The app is pulling data from a GraphQL API linked to a CMS, and everything is functioning properly. Whenever content is updated in ...

What is the process for creating a server-side API call?

I've designed a front-end application that uses an API to retrieve data. The problem I'm facing is that in order to access the API, I need to use an API Key. If I include the API key in the client-side code, it will be visible to users. How can I ...

What is the best way to customize the default button style for buttons in Angular Datables?

After integrating buttons into my Angular Datatables, I noticed that they have default styling, which makes them stand out from the rest of my web page (refer to the Column Visibility button in the screenshot below): I attempted to use CSS to make the but ...

The Ajax form is failing to send any headers

Whenever I submit my form, the header data doesn't seem to be coming through. Even though I've done this type of submission numerous times (figuratively speaking), there's always a chance that I might be overlooking something. Any ideas? Che ...

Determine the absolute path with respect to a different reference path, rather than the current working directory

One of the challenges I'm facing with my Node.js module is how to easily allow developers to edit the location of the dependencies relative to the module file itself. The issue arises because the current working directory, accessed through `process.cw ...

Is there a way to extract a username from LDAP?

Can you help me understand how to dynamically retrieve a username from LDAP? In the code snippet below, I have hardcoded the username as 'smith2': $_SERVER["REMOTE_USER"] = 'smith2'; $param = $_SERVER["REMOTE_USER"] By using this appr ...

Examining a feature by solely utilizing stubs

I've been immersed in writing tests for the past few weeks. In my workplace, we utilize Mocha as our test runner and Chai for assertions, with Sinon for creating stubs. However, there's a recurring issue that's been bothering me. I've w ...

Unable to receive notifications within an AngularJS service

<!DOCTYPE html> <html> <script src="http://ajax.googleapis.com/ajax/libs/angularjs/1.3.14/angular.min.js"></script> <body> <div ng-app="canerApp" ng-controller="canerCtrl"> <button ng-click="click()"> ...

In React JS, the data from my response is not being saved into the variable

My goal is to store the response data in the variable activityType. Within the useEffect function, I am iterating over an API based on the tabs value. The API will return a boolean value of either true or false. While I can successfully log these values ...

Ways to initiate a page redirection within the componentWillReceiveProps lifecycle method

When my webpage or component generates a form and sends it to the backend API upon submission, I receive an object in return if the process is successful. This object is then added to my redux store. In order to determine whether the reducer successfully ...

Expanding and shrinking the index within a specific circular boundary in JavaScript

I'm dealing with a circular range of ASCII values from a to z, where each letter corresponds to a number between 97 and 122. I have no issue with increasing the value within this range, but I am struggling when it comes to decreasing it. For example ...

Is there a way to dynamically enable ui-sref through element.append in Angular?

I am in the process of developing a pagination directive. Once the total_for_pagination data is filled, the appropriate HTML for the pagination gets generated. Here's my current HTML structure with a maximum of 50 per page: <pagination data-numbe ...

Incorporating an NPM module into a React file: Webpack encounters resolution issues

After reviewing information from this source and here, the process of publishing a react module to NPM and then using it in another project while having the component in the node_modules directory should be as follows: Create and export a module Specify ...

Facing a NodeJS 404 error while working on the Angular state API with UI-Router

I am currently working on a nodejs angular app. The Nodejs server is up and running smoothly. I have set up ui router with html5mode enabled. When I try to access http://localhost:3000/superadmin, Nodejs returns a 404 error. However, if I access the rout ...

Can someone explain the method for displaying or concealing a menu based on scrolling direction?

https://i.stack.imgur.com/tpDx0.jpg I want to hide this menu when scrolling down and show it when scrolling up. The code for my menu bot is: <script> var previousScroll = 0; $(window).scroll(function(event){ v ...

Issues connecting a model to a custom directive in Angular

I have been working on getting two multiselect widgets to sync with each other, but have only succeeded in achieving one-way synchronization. One widget is located on the main page, while the other is in an angular ui-bootstrap modal. Both multiselect wid ...

Exploring the nuances of receiving responses with NextJs, Nodemailer, and Fetch

Currently in the process of constructing a basic contact form with Next.js, nodemailer, and fetch. Despite successfully sending emails from the frontend form to the designated email account, the network shows the contact submission as pending. After approx ...

Issues with clicking in JS eventListener

I'm in need of some assistance with a small header issue in my project. I've included all the necessary header files so that you can run it in a snippet and see what's going on. The problem lies within my JS file. When running the file, you ...

What is causing the Unhandled Rejection (TypeError) error after I move the object in my Redux application and receive the message "Cannot read property 'filter' of undefined"?

I've been working on a Redux application. I've made modifications to a couple of files, and here are the changes in the two files: index.js: const store = createStore( reducer, { propReducer: { day: 1, data: [], filte ...

Encountering a 404 error in an AngularJS app within Cordova on Android

Currently, I am working on an android application using Cordova and AngularJS (front-end framework OnsenUI). Below is the code snippet for my GET request to retrieve data from the server: $http.get(url+"getlotterylist").then(function(msg){ $scope. ...