I'm trying to include the below bootstrap collapsible panel in my angular application. However, when I click on "Expand", angular seems to see href="#collapseOne"
and then redirects to the home page instead of collapsing the panel. My routing looks like this, and I think the otherwise({redirectTo: '/home'});
is causing the problem. Any suggestions?
angular.module('App')
.config(['$routeProvider', function($routeProvider) {
$routeProvider.
when('/users', {templateUrl: 'partials/users/user-list.html', controller: 'UserCtrl'}).
when('/users/new', {templateUrl: 'partials/users/user-new.html', controller: 'UserNewCtrl'}).
when('/users/:userid', {templateUrl: 'partials/users/user-detail.html', controller: 'UserDetailCtrl'}).
otherwise({redirectTo: '/home'});
}]);
The panel-
<div class="panel-group" id="accordion">
<div class="panel panel-default">
<div class="panel-heading">
<h4 class="panel-title">
<a data-toggle="collapse" data-parent="#accordion" href="#collapseOne">
Expand
</a>
</h4>
</div>
<div id="collapseOne" class="panel-collapse collapse in">
<div class="panel-body">
...
</div>
</div>
</div>
</div>
As mentionned in a similar question Here, simply change your href by the data-target attribute
I had the same issue, and I didn't want to add an additional library beyond bootstrap. As recommended, you can use
data-target="#your-collapsing-element"
, removing thehref
attribute completely.The basics
To make a panel collapse you need:
On the element used to trigger collapse:
On the element that collapses:
On the element wrapping both the "trigger" element and the collapsing element:
Putting it all together
You can optionally add parent elements to make it part of a group, and add add the css classes "
collapse in
" to the collapsing element to make its default state closed. A full example:You can get better button behaviors by using a
<button>
element rather than an<a>
. Overriding bootstrap's styling for.panel
can be done by adding your own styling for thepanel
css class.<div class="panel its-my-panel">
href
in order to expand-collapse todata-target
.data-target
andid
of thediv
to be expanded and collapsed should not contain hyphen.You can use the accordion directive from AngularJS UI Bootstrap that builds on top of twitter bootstrap the collapse directive.
example:
Live example: http://jsfiddle.net/choroshin/U89bW/3/
Click the buttons below to show and hide another element via class changes:
collapse hides content
collapsing is applied during transitions
collapse.in shows content
You can use a link with the href attribute, or a button with the data-target attribute. In both cases, the data-toggle="collapse" is required.
check this example below.
Bootsrap Example