Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular6 and Bootstrap4 collapsible div does not work

There is accordion control provided with bootstrap4 https://getbootstrap.com/docs/4.0/components/collapse/#accordion-example

When I try to use this with my angular 6 app , it does not collapse. There is no error in the browser but it does not work. Any idea on this one ?

html

<div class="accordion" id="accordionExample">
  <div class="card">
    <div class="card-header" id="headingOne">
      <h5 class="mb-0">
        <button class="btn btn-link" type="button" data-toggle="collapse" data-target="#collapseOne" aria-expanded="true" aria-controls="collapseOne">
          Collapsible Group Item #1
        </button>
      </h5>
    </div>

    <div id="collapseOne" class="collapse show" aria-labelledby="headingOne" data-parent="#accordionExample">
      <div class="card-body">
        Anim pariatur cliche reprehenderit, enim eiusmod high life accusamus terry richardson ad squid. 3 wolf moon officia aute, non cupidatat skateboard dolor brunch. Food truck quinoa nesciunt laborum eiusmod. Brunch 3 wolf moon tempor, sunt aliqua put a bird on it squid single-origin coffee nulla assumenda shoreditch et. Nihil anim keffiyeh helvetica, craft beer labore wes anderson cred nesciunt sapiente ea proident. Ad vegan excepteur butcher vice lomo. Leggings occaecat craft beer farm-to-table, raw denim aesthetic synth nesciunt you probably haven't heard of them accusamus labore sustainable VHS.
      </div>
    </div>
  </div>
</div> 

**** EDIT ****

Here is live code example https://stackblitz.com/github/Rugved/ng6bt4Demo/

***** EDIT **** jquery included in angular.json file

        "styles": [
          "src/styles.css",
          "node_modules/bootstrap/dist/css/bootstrap.min.css"
        ],
        "scripts": [
          "node_modules/jquery/dist/jquery.min.js",    
          "node_modules/popper.js/dist/umd/popper.min.js",
          "node_modules/bootstrap/dist/js/bootstrap.js"                        
        ]
like image 877
Zeus Avatar asked Sep 18 '18 23:09

Zeus


People also ask

How do you make a div collapse?

The .collapse class indicates a collapsible element (a <div> in our example); this is the content that will be shown or hidden with a click of a button. To control (show/hide) the collapsible content, add the data-toggle="collapse" attribute to an <a> or a <button> element.

Why bootstrap is not working on angular?

If you are adding the bootstrap path into the angular. json file, make sure it is the styles within the project\architect\build. Not the one in the project\architect\test.

How do you expand and collapse in bootstrap?

Just add data-toggle="collapse" and a data-target to the element to automatically assign control of one or more collapsible elements. The data-target attribute accepts a CSS selector to apply the collapse to. Be sure to add the class collapse to the collapsible element.


2 Answers

Your code works perfectly fine for me.

Please find the below configurations

package.json

{
"bootstrap": "^4.1.3",
"popper.js": "^1.14.3",
"jquery": "3.3.1"
}

angular.json

"styles": [
              "src/styles.scss",
              "node_modules/bootstrap/dist/css/bootstrap.min.css"
            ],
            "scripts": [
              "node_modules/jquery/dist/jquery.min.js",
              "node_modules/bootstrap/dist/js/bootstrap.min.js"
            ]
like image 138
Pratap A.K Avatar answered Oct 20 '22 13:10

Pratap A.K


You need to include jQuery for many of the components in bootstrap to function.

Checkout this pen, if you comment jQuery, collpase does not work, when you uncomment it works.

From bootstrap docs

Many of our components require the use of JavaScript to function. Specifically, they require jQuery, Popper.js, and our own JavaScript plugins. Place the following s near the end of your pages, right before the closing tag, to enable them. jQuery must come first, then Popper.js, and then our JavaScript plugins.

The bootstrap package you've included does not contain jQuery. From npm,

Bundled JS files (bootstrap.bundle.js and minified bootstrap.bundle.min.js) include Popper, but not jQuery.

like image 37
SubSul Avatar answered Oct 20 '22 11:10

SubSul