Bootstrap collapse and ui-router

2020-04-07 20:16发布

问题:

I have issue with ui-router and using bootstrap collapse.

<div class="panel panel-default" id="accordion" >
    <div role="tab" id="headingOne">
       <a role="button" data-toggle="collapse" data-parent="#accordion" href="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
            Click me
        </a>
    </div>
    <div id="collapseOne" class="panel-collapse collapse" role="tabpanel" aria-labelledby="headingOne">
            Data
    </div>
</div>

This is the page localhost/#/root/mypage/. So the 'Click me' is pointed to localhost:\#collapseOne that leads me to default page of my site.

回答1:

You could simply fix you issue by using data-target attribute(will do the same work) instead of using href attribute which is affecting routing

Markup

<div class="panel panel-default" id="accordion" >
    <div role="tab" id="headingOne">
       <a role="button" href="javascript:;" data-toggle="collapse" data-parent="#accordion" 
          data-target="#collapseOne" aria-expanded="false" aria-controls="collapseOne">
            Click me
        </a>
    </div>
    <div id="collapseOne" class="panel-collapse collapse" role="tabpanel"
     aria-labelledby="headingOne">
        Data
    </div>
</div>